0

After an introduction to Angular.js at CodeSchool via FreeCodeCamp, I am coding a zipline where I have to Stylize Stories on Camper News. Basically, I am making a call to an API and need to display the result.

However, I have difficulties displaying the result of my API call with angular.js. I searched on the web and on StackOverflow and could not find a way to fix my issue.

Here is my app.js:

    (function() {
  var app = angular.module('camperNews', []);

  app.controller('StoryController', ['$http', function($http) {
    var story = this;
    story.news = [];
    $http.get('http://www.freecodecamp.com/news/hot').
    then(function(response) {
      story.news = response;
    }, function(err) {
      console.error('ERR', err);
    });
  }]);
})();

And my HTML:

<body ng-controller="StoryController as story">
    <div id="container" class="text-center">
        <div id="posts" class="text-center" ng-repeat="new in story.news">
            <div class="post">
                <div class="author">
                    <a href="{{new.link}}" target="_blank"><img class="author-picture" src="{{new.author.picture}}"></a>
                </div>
            </div>
        </div>
    </div>
  </body>

I looked in the console and my call is returning something, the problem is obviously in my HTML and caused by my lack of experience. I would help to find a solution or hints on how to fix my issue.

Thank you in advance.

1
  • 2
    try using story.news=response.data in the .then block. Commented Sep 29, 2015 at 1:53

5 Answers 5

1

You need to change <div id="posts" class="text-center" ng-repeat="new in story.news"> to <div id="posts" class="text-center" ng-repeat="new in story.news.data">.

Alternatively you could change story.news = response; in your controller to story.news = response.data;

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

Comments

1

Using then() with $http the callback response argument is a more complex object than just your data ... data is a property of that object:

$http.get('http://www.freecodecamp.com/news/hot').then(function(response) {    
  story.news = response.data;
}, function(err) {
  console.error('ERR', err);
});

1 Comment

Thanks for the explanation.
0
  1. Try to run it without the (function(){})()

2.It seems like you're using the $http.get to access an api server with http ajax tool, but you rather use an api client as a part of angular providers.

$resource is a provider made exactly for this kind of usages..

here is factory file (news.factory.js)

angular 
           .module('camperNews')
           .factory('$news',newsFactory);

//Injects strings to function dep.
newsFactory.$injector('$resource');

newsFactory($resource){

var baseUrl = 'http://www.freecodecamp.com/news';

return  
 $resource(baseUrl+'/hot', 
   {/*params dictionary*/},
     /*This is get/post/delete/put and so on customizations..*/
     {get: {method:'GET', params:{}, isArray:true /*i think you get a list..*/}
   );            
}

Here is my app.js:

  angular.module('camperNews', ['$news'/*TODO:Add ng-resource*/]);

  angular 
       .module('camperNews')
       .controller('StoryController', ['$news', function($news) {
    var story = this;
    story.news = [];

/*Will load a resource, with the array 
*and $resolved(bool) and $promise(promise type) and etc..
*/
    story.news = $news.get();
  }]);

And my HTML:

<body ng-controller="StoryController as story">
    <div id="container" class="text-center">
        <div id="posts" class="text-center" ng-repeat="new in story.news">
            <div class="post">
                <div class="author">
                    <a href="{{new.link}}" target="_blank"><img class="author-picture" src="{{new.author.picture}}"></a>
                </div>
            </div>
        </div>
    </div>
  </body>

Comments

0

why don't you use $scope? instead of var story = this; write $scope.story = {}; and $scope.story.news = response; and write ng-controller="statecontroller".

1 Comment

you use this in controller when using controllerAs alias and syntax in view
0

in this case $resource service would be better https://docs.angularjs.org/api/ngResource/service/$resource

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.