如何使用angularjs找到嵌套的json对象?

时间:2014-02-20 14:52:32

标签: javascript json angularjs

如何使用AngularJS找到一个相当嵌套的JSON对象并输出?我已经搞砸了很长一段时间,但我似乎无法做到正确。希望你们能帮助我!

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: 我正在尝试获取帖子标题和计数器

{
"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"
            }
        ]
    }
}

控制:

'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;
});
}]);

1 个答案:

答案 0 :(得分:2)

而不是

 <div ng-repeat="post in posts">

使用

 <div ng-repeat="post in posts.data.posts">

您也可以修改Controller并使用现有的HTML

控制器

$http({method:'POST', url: 'jsonData.php', headers: {}})
.success(function(data) {
    $scope.posts = data.data.posts;
});