2015-09-25 144 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