2015-09-25 145 views
0

我目前有一个JSON数组,我试图通过使用循环向屏幕输出。目前我可以在没有循环的情况下得到我想要的结果,但是当我尝试将其转换为循环时,我遇到了一些问题。任何见解都会有所帮助。谢谢。角度循环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> 

我的业绩为先“的选择”是正确的,但第二和第三都没有结果。

这里是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"}} 
+0

你可以发布控制器代码或至少是什么切换数组看起来像?你也可以澄清你的代码的哪部分工作,哪部分不工作? – o4ohel

+1

所有的选项都缺少'value'属性.. 3rd应该是正确的。虽然我建议你去使用'ng-options' –

+0

@ o4ohel:控制器代码根本没有帮助,因为它是一个API响应。第一个SELECT工作,但另外两个不工作。我刚刚更新了问题以显示切换结构。 –

回答

1

你应该纠正你的JSON结构&不应该有 “0”, “1” & “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> 
+1

这是一个很好的答案。但是,如果您不想将对象预处理为数组,则可以使用以下语法: o4ohel

+0

@ o4ohel谢谢bro ..我更新了答案.. –

+0

为了记录,我同意@ pankaj-parkar说'越区切换'是一个数组更好/更有意义。 – o4ohel