ng-repeat (iterate) over complex json object in Angular

早过忘川 提交于 2019-12-12 06:19:21

问题


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..


回答1:


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



来源:https://stackoverflow.com/questions/30944618/ng-repeat-iterate-over-complex-json-object-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!