Reputation: 29
How can I find a rather nested JSON object and output in with AngularJS? I've been messing with it for quite some time now but I just can't seem to get it right.. Hope you guys can help me out!
HTML:
<div id="ng-app" ng-controller="AnalyticsCtrl">
<div ng-repeat="post in posts">
<span>{{post.title}}</span>
<span>{{post.counter}}</span>
</div>
</div>
JSON: I'm trying to fetch the post title, and the counter
{
"status" : "success",
"data" :
{
"activeVisitors" : "148",
"posts" :
[
{
"id" : 1,
"title" : "Bla blabla blablabl bla blablaba",
"counter" : "20"
},
{
"id" : 2,
"title" : "Blie bla blup wflup flel del",
"counter" : "18"
},
{
"id" : 3,
"title" : "Flel djep flep tro fro klel",
"counter" : "14"
}
]
}
}
Ctrl:
'use strict';
var myApp = angular.module('myApp', []);
myApp.controller('AnalyticsCtrl', ['$scope', '$http', function($scope,$http) {
$http({method:'POST', url: 'jsonData.php', headers: {}})
.success(function(data) {
$scope.posts = data;
});
}]);
Upvotes: 1
Views: 4470
Reputation: 133403
Instead of
<div ng-repeat="post in posts">
use
<div ng-repeat="post in posts.data.posts">
OR
You can alternatively modify Controller and use your existing HTML
Controller
$http({method:'POST', url: 'jsonData.php', headers: {}})
.success(function(data) {
$scope.posts = data.data.posts;
});
Upvotes: 2