I am learning how to use angular and I am not very good at making api request. I am trying to use http://api.football-data.org/index . The json data I wanted to get from my angular factory is http://api.football-data.org/v1/competitions/426/leagueTable . I am getting an error in the console right now
'angular.js: 13920 TypeError: Unable to read getLeagueData property from undefined on new ...'
My CLI shows that I load all my script files, and I tested my controller before trying to connect to the factory and creating the getLeagueData function, and it works, so I know that my problem occurs after creating the base controller, I thought that this may be due to my headers needing authentication tokens that I received, but I'm not sure that I did not add it correctly. Here is my code
HTML
<!DOCTYPE html>
<html lang="en" ng-app='bplApp'>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><%= title %></title>
<link href="/bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="/bower_components/font-awesome/css/font-awesome.min.css">
<link rel='stylesheet' type='text/css' href='/stylesheets/main.css'>
</head>
<body>
<div class='leagueCheck' ng-controller='tableController as table'>
<div class='container'>
<div class='row'>
<div class='col-xs-12'>
{{table.test}}
</div>
</div>
</div>
</div>
<script src="/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src='/bower_components/angular/angular.min.js'></script>
<script src='bplApp.js'></script>
<script src='/controllers/tableController.js'></script>
<script src='/services/footballData.js'></script>
Module
var app = angular.module('bplApp',[]);
controller
app.controller('tableController', ['$scope', 'footballData', function($scope, footballData){
var self = this;
self.test = 'is working';
self.leagueStats = [];
footballData.getLeagueData().then(function(data){
self.leagueStats = data;
console.log(self.leagueStats);
})
}])
Factory
app.factory('footballData', [function($http){
return {
getLeagueData: function(){
return $http({
method: 'GET',
url: 'http://api.football-data.org/v1/competitions/426/leagueTable',
headers:{
'X-Auth-Token': '3e629af30fce46edaa6ead20e007a276'
}
})
}
}
}])
The original ajax code example that shows the api to use it is as follows:
$.ajax({
headers: { 'X-Auth-Token': 'YOUR_API_TOKEN' },
url: 'http://api.football-data.org/v1/fixtures?timeFrame=n1',
dataType: 'json',
type: 'GET',
}).done(function(response) {
var regex = /.*?(\d+)$/;
var res = regex.exec(response.fixtures[0]._links.awayTeam.href);
var teamId = res[1];
console.log(teamId);
});