1

I am very new to AngularJS and want to get started retrieving data from a remote source.

My app.js file looks like this:

var app = angular.module('footy', []);

app.controller('ClubController', ['$http', function($http){

    var club = this;
    club.team = [];

    $http.get('http://api.football-data.org/teams/19').success(function(data){
        club.team = data;
    });
}]);

and my html looks like this:

<!doctype html>
<html ng-app="footy">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script src="app.js"></script>
</head>
<body>

  <div ng-controller = "ClubController as data">
    {{data.club.team.name}}
  </div>
</body>
</html>

When I view the page in a browser all that is returned is a blank page and in the source I can see "{{data.club.team.name}}" between the div.

When I view the data source Url in a browser I see this:

{

"id": 19,
"name": "Eintracht Frankfurt",
"shortName": "Eintr. Frankfurt",
"crestUrl": "http://upload.wikimedia.org/wikipedia/commons/0/04/Eintracht_Frankfurt_Logo.svg"
}

I have gone through and completed this course: http://campus.codeschool.com/courses/shaping-up-with-angular-js/ and was trying to apply the instructions from chapter 5 about services to this but I am having no luck.

Can someone help? Thanks in advance.

11
  • <div ng-controller = "ClubController as data"> should be <div ng-controller="ClubController as data"> Commented May 15, 2015 at 20:47
  • What does the console say? Commented May 15, 2015 at 20:50
  • api.football-data.org/teams/19 doesn't allow CORS... Commented May 15, 2015 at 20:50
  • Well I think aarong gave the answer Commented May 15, 2015 at 20:52
  • @aarong looks like it does api.football-data.org/register i have an API key. Commented May 15, 2015 at 20:53

1 Answer 1

2

I see two problems here:

  • http://api.football-data.org/teams/19 doesn't allow CORS. You can't load the data. Update: The OP pointed out that s/he has an API key that allows CORS. This is not the issue then, but might be for other people trying to reproduce this.
  • {{data.club.team.name}} should be {{data.team.name}} because you said var club = this;. In other words, the controller you aliased data is the same as the club object. The club object is not a property of the controller data, it IS the controller's data.

See this plnkr for a demo. The commented out code doesn't work because of the api key, the locally hosted file does: http://plnkr.co/edit/ItD96Zfk0g1cLyGrrgBy?p=preview

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.