简体   繁体   English

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

[英]How to find nested json object with angularjs?

How can I find a rather nested JSON object and output in with AngularJS? 如何找到一个相当嵌套的JSON对象并通过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: 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 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"
            }
        ]
    }
}

Ctrl: 按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;
});
}]);

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并使用现有的HTML

Controller 调节器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM