2015-09-03 61 views
0

我是angular JS的新手。 我從Web服務調用中獲取下面的JSONArray。要求是顯示子數組「選項」和「文本」中的值。我的下拉菜單應顯示爲從下面生成HTML:使用angularjs顯示覆雜JSON的下拉值

<select> 
<option value="L1" > English </option> 
<option value="L2" > French </option> 
<option value="L3" > Chainese </option> 
</select> 

這裏是我一直在使用的角度NG選項試圖代碼:

<td data-ng-repeat="field in json"> 
    <select tabindex="0" data-ng-options="option.code as option.code as option.text.label for option in field.options" data-ng-model="language_code"> 
     <option value=""> Select...</option> 
    </select> 

我不能從基礎對象訪問「選項」或「文本」數組。

[{ 
    "id": null, 
    "code": "Language", 
    "order": 1, 
    "options": [ 
     { 
      "code": "L1", 
      "order": 1, 
      "text": [ 
       { 
        "language": "eng", 
        "label": "English", 
       } 
      ] 
     }, 
     { 
      "code": "L2", 
      "order": 2, 
      "text": [ 
       { 
        "language": "fre", 
        "label": "French", 
       } 
      ] 
     }, 
     { 
      "code": "L3", 
      "order": 3, 
      "text": [ 
       { 
        "language": "chn", 
        "label": "Chainese", 
       } 
      ] 
     } 
    ] 
}] 

請建議解決方案。

回答

0

你有奇怪的JSON格式: 這裏工作的標記和plnker有完整的解決方案:http://plnkr.co/edit/OFyfJkTSgpoW2Gwkzzda

<table> 
    <tr> 
     <td ng-repeat="field in json"> 
      <select tabindex="0" ng-model="selected" data-ng-options="option.code as option.text[0].label for option in field.options"> 
       <option value=""> Select...</option> 
      </select> 
     {{selected}} 
     </td> 
    </tr> 
</table> 
+0

其很多 –

+0

工作的感謝,如果我們有多個文本數組字段(option.text [0] .label)他們有沒有其他方法可以訪問標籤? –

+0

不知道你的問題是什麼......在你提供的json中,有一個數組字段「text」,但是你只提供了一個元素。因此,我將它綁定到該數組的第一個([0])元素...如果只有一個數組元素,則可以將結構展平,並將代碼/順序/語言/標籤保留在一個級別中。或者,您可以將其他條目添加到該數組,然後按索引訪問,例如[1]。 – JanisP