I am trying to parse data from a json file and display it in my angular application, but for some odd reason the view wouldn't show the data(intepret my expressions), I thought it might be my controller, but it looks fine. Any help would be appreciated in solving this, thanks.
Below is the code for my Http Service:
app.factory('info', ['$http', function($http){
return
$http.get('https://api.flickr.com/services/feeds/photos_public.gne?&format=json&callback=JSON_CALLBACK')
.success(function(data){
return data;
})
.error(function(err){
return err;
});
}]);
Here is my controller:
app.controller('HomeController', ['$scope','info',
function($scope) {
info.success(function(data){
$scope.datar = data;
});
}]);
And this is my view:
<div class="main" >
<div class="container" >
<div class="photo" >
<div ng-controller="HomeController" ng-repeat="data in datar">
<p1>{{data.age}}</p1>
<p1>{{name.modified}}</p1>
</div>
</div>
</div>
</div>
The $http service will return a response having following properties:
also I think your factory should look like this:
and in the controller
if you are using google chrome you could try right click -> inspect element, you will see if there are some errors, or you could put breakpoints in javascript to see exactly what data you receive.
I recommend as further reading:
https://docs.angularjs.org/api/ng/service/$http
http://weblogs.asp.net/dwahlin/using-an-angularjs-factory-to-interact-with-a-restful-service
Since you are querying JSONP endpoint you need to use $http.jsonp method. And very important: put
$http.jsonp
on the same line afterreturn
, otherwise automatic semicolon insertion turns it intoreturn;
basically returningundefined
.Note parameter name
jsoncallback
, not just "callback".After that your controller should also use
then
instead of success (it's deprecated):Demo: http://plnkr.co/edit/yFomQ8jOZDRS6mZSBkGx?p=preview
As far as I can see u are returning data. In AngularJS there are no synchronous calls.
You need to return a promise
But then you also have to change your controller to: