Angular循环JSON数组

时间:2015-09-25 18:04:29

标签: arrays json angularjs

我目前有一系列JSON,我试图通过循环打印到屏幕上。目前我可以在没有循环的情况下获得我想要的结果但是当我尝试将其转换为循环时遇到了一些问题。任何见解都会有所帮助。谢谢。

这是我目前的代码:

<div>
    <h2>Document Type</h2>
    <select>
        <option>Document Type : {{handoff[0].documentType}}</option>
        <option>Document Type : {{handoff[1].documentType}}</option>
        <option>Document Type : {{handoff[2].documentType}}</option>
        <option>Document Type : {{handoff[3].documentType}}</option>
        <option>Document Type : {{handoff[4].documentType}}</option>
    </select>
    <select>
        <option ng-repeat="temp in handoff">Document Type : {{temp[0].documentType}}</option>
    </select>
    <select>
        <option ng-repeat="temp in handoff">Document Type : {{temp.documentType}}</option>
    </select>
</div>

第一个&#34;选择&#34;的结果是正确的,但第二和第三没有结果。

以下是obj的样子: enter image description here

"handoffs":{"0":{   "documentType":"0","conlID":"1230","userName":"CU0","handoff":"h=81878E9E772BCA176D868CA633BFB47D38274B1B209FD80856E56B47"
},"1":{"documentType":"1","conlID":"C010","userName":"A010","handoff":"ERROR: A temporary problem was encountered.  Please try your request again in a few minutes."},"3":{"documentType":"3","conlID":"C010","userName":"C10","handoff":"HANDOFF=81878E9E77FB56E56B47"}}

1 个答案:

答案 0 :(得分:1)

你应该纠正你的json结构&amp;不应该有“0”,“1”和&amp; <2>在您的回复中的对象文字中,您可以简单地拥有一个数组,以便您的问题得到解决。

更新了JSON

{
    "handoffs": [{
            "documentType": "0",
            "conlID": "1230",
            "userName": "CU0",
            "handoff": "h=81878E9E772BCA176D868CA633BFB47D38274B1B209FD80856E56B47"
        },{
            "documentType": "1",
            "conlID": "C010",
            "userName": "A010",
            "handoff": "ERROR: A temporary problem was encountered.  Please try your request again in a few minutes."
        },{
            "documentType": "3",
            "conlID": "C010",
            "userName": "C10",
            "handoff": "HANDOFF=81878E9E77FB56E56B47"
        }]
    }
}

<强>更新

如果您对数据没有任何控制权,那么@ o4ohel建议一个不错的选择。不想改变它。然后你应该使用选项

<select>
    <option ng-repeat="(key, value) in handoff">Document Type : {{value.documentType}}</option>
</select>