ng-repeat(迭代)Angular 中复杂的 json 对象 [英] ng-repeat (iterate) over complex json object in Angular

查看:30
本文介绍了ng-repeat(迭代)Angular 中复杂的 json 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个控制器返回一个非常复杂的 json 对象:

<代码>{184":{"title": "石头玫瑰",会议":{1443564000000":{数量":5},1442959200000":{数量":1},1447196400000":{数量":1},1444428000000":{数量":2},1446332400000":{数量":3}}}

}

我试图像这样在我的视图中迭代它

<h1>{{item.sessions}}</h1>

使用此代码,如果对象打印在 html 响应中,我会得到一部分:

{"1443564000000":{"num":1}}

但是如你所见,sessions有一个非常复杂的属性(我用session的id来存储它的数量)

我已经看到有些人曾经用 ng-repeat-start 来做到这一点,在我的情况下,使用它会给我带来几个错误..

解决方案

假设您要迭代 sessions 属性,该属性不是数组而是对象,则必须迭代对象的属性:

这是一个例子:http://plnkr.co/edit/7AQF6k7hf2aZbWFmhVoX?p=预览

示例来自的相关 SO 问题:如何在 ng-repeat 中迭代键、值

One controller returns me a very complex json object:

{
"184": {
    "title": "THE STONE ROSES",
    "sessions": {
        "1443564000000": {
            "num": 5
        },
        "1442959200000": {
            "num": 1
        },
        "1447196400000": {
            "num": 1
        },
        "1444428000000": {
            "num": 2
        },
        "1446332400000": {
            "num": 3
        }
    }
}

}

I have tried to iterate over it on my view like this

<div class="large-6 columns" ng-repeat="item in showItems" st-raw>
    <h1>{{item.sessions}}</h1>
 </div> 

with this code I get part if the object printed on the html response:

{"1443564000000":{"num":1}}

But as you can see, sessions has a very complex attribute (I use the id of the session to store the number of it)

I have seen that some people used to do that with ng-repeat-start, on my case using it gives me severals errors..

解决方案

Assuming that you want to iterate over the sessions property, which is not an array but an object, you would have to iterate over the object's properties:

<tr ng-repeat="(key, value) in data">

Here is an example: http://plnkr.co/edit/7AQF6k7hf2aZbWFmhVoX?p=preview

relevant SO questions where the example is from: How can I iterate over the keys, value in ng-repeat in angular

这篇关于ng-repeat(迭代)Angular 中复杂的 json 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆