如何将ng-repeat转换为ng-options?

时间:2014-12-04 09:01:03

标签: angularjs ng-options

我没有得到ng-model如何连接到ng-repeat选项。如何将下面的代码转换为ng-repeat?我可以得到ng-repeat工作但是

  

ng-selected =" {{alternative.code == question.answer}}"

是我不了解如何处理ng-options的原因。

<select ng-model="question.answer">
   <option ng-selected="{{alternative.code == question.answer}}"
            ng-repeat="alternative in question.alternatives"
            value="{{alternative.code}}">{{alternative.label}}</option>
</select>    

2 个答案:

答案 0 :(得分:1)

使用以下内容:

<select ng-model="question.answer"
        ng-options="alternative.code as alternative.label for alternative in question.alternatives">
</select>

如果question.alternatives是一个数组,则以下内容:

<select ng-model="question.answer"
        ng-options="alternative.code as alternative.label for (key, alternative) in question.alternatives">
</select>

如果是对象。两个表达式都以语法value as label开头,它告诉<select>要考虑的值(绑定到模型)以及用作标签的内容(用户可见)。然后,自动完成“此选项具有该值并且在模型具有该选项时选择”的整个业务。 See documentation.

答案 1 :(得分:0)

<select ng-model="question.answer" ng-options="alt.code for alt in question.alternatives"></select>

modelValue as optionLabelValue for array in item 如果' modelValue as '是ommited,则modelValue被视为项目对象