Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
});
}]);
share|improve this question
3  
in place of post in posts use post in YourVar.data.posts –  Satpal Feb 20 '14 at 14:55
1  
Show AnalyticsCtrl controller –  Ilan Frumer Feb 20 '14 at 14:57
    
Added controller –  Geert Van de Langenberg Feb 20 '14 at 15:02

1 Answer 1

up vote 1 down vote accepted

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;
});
share|improve this answer
    
Thanks for the info! Might there be any way to multiply (or do any kind of maths) the counter with the activeVisitors in the same ng-repeat? –  Geert Van de Langenberg Feb 20 '14 at 15:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.