1

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;
});
}]);
2
  • 3
    in place of post in posts use post in YourVar.data.posts Commented Feb 20, 2014 at 14:55
  • 1
    Show AnalyticsCtrl controller Commented Feb 20, 2014 at 14:57

1 Answer 1

2

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;
});
Sign up to request clarification or add additional context in comments.

1 Comment

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?

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.