当我尝试在init上设置下拉列表的选定值时,它不起作用。选定的值来自注入控制器的工厂。但是,当我做一个非常简单的例子时,它不起作用,请参阅:JSFiddle
<div ng-init="selectedItem1 = '2';selectedItem2 = '2';items=[{id:1,name:'One'},{id:2,name:'Two'},{id:3,name:'Three'}];">
<select ng-model="selectedItem1">
<option></option>
<option ng-repeat="item in items" value={{item.id}}>{{item.name}}</option>
</select>
{{selectedItem1}}
<br />
<select ng-options="i.name for i in items track by i.id" ng-model="selectedItem2">
</select>
{{selectedItem2}}
<button ng-click="selectedItem1=null;selectedItem2=null">Reset</button>
在两种情况下都不起作用,使用ng-options和使用ng-repat。
我做错了吗?
答案 0 :(得分:0)
使用ng-selected属性确定所选选项。另外,将selectedItem1声明为int,而不是字符串。
<div ng-init="selectedItem1 = 2; items=[{id:1,name:'One'},{id:2,name:'Two'},{id:3,name:'Three'}];">
<select ng-model="selectedItem1">
<option></option>
<option ng-repeat="item in items" ng-selected="item.id === selectedItem1" value={{item.id}}>{{item.name}}</option>
</select>