ng-options无法按预期工作

时间:2015-07-14 16:49:51

标签: angularjs

我在尝试使用ng-options循环遍历对象数组时遇到了问题。

HTML

<select class="select-width" ng-options="type as type.name for type in types">

JS

$scope.types = [{name: "Advanced Yield Analysis"}, 
            {name: "Yield by Hybrid"},
            {name: "Yield by planting data"},
            {name: "Yield by soil type"},
            {name: "Yield by management zone"},
            {name: "Yield by population/seeding rate"},
            {name: "Yield by Treatment"},
            {name: "Total Yield by Grower / Location / MC"}];

这是JS-fiddle

1 个答案:

答案 0 :(得分:1)

您只需要传递ng-model即可。当你没有分配模型时,Ng选项将不起作用。

  

默认情况下,ngModel通过引用而非值来监视模型。这是   将任何输入指令绑定到一个模型时很重要   对象或集合。

<select class="select-width" ng-model="demo" ng-options="type as type.name for type in types"></select> 

Fiddle