设置下拉列表的默认值
Setting default value of dropdown
请看下面jsbin
我创建了一个使用模板的下拉列表类型(这样我就可以添加 SELECT ONE
选项)。这样做之后,我似乎不能再默认下拉菜单了。
根据这个 example 这应该设置默认值:
vm.model = {dropdown1: 'key1Key'};
这会在模型中将 dropdown1
设置为 key1Key
...但未选择默认值。我错过了什么?
您在 ng-options 中的值对于您要实现的目标不正确。
我建议尝试:
option.Key as option.Value for option in to.options
据我所知,您应该使用 ng-model 来设置下拉列表的默认值。
分配给 ng-model 的变量值应该包含选项的值。
例如
vm.list = [{Id : 1, Text : 'Text 1'},{Id : 2, Text : 'Text 2'}];
vm.selected = 1; // Id used as value so, set to 1 to assign it as default.
然后,
<select ng-model="vm.selected">
<option ng-repeat="option in vm.list" value="{{option.Id}}">{{option.Text}}</option>
</select>
希望对您有所帮助。
请看下面jsbin
我创建了一个使用模板的下拉列表类型(这样我就可以添加 SELECT ONE
选项)。这样做之后,我似乎不能再默认下拉菜单了。
根据这个 example 这应该设置默认值:
vm.model = {dropdown1: 'key1Key'};
这会在模型中将 dropdown1
设置为 key1Key
...但未选择默认值。我错过了什么?
您在 ng-options 中的值对于您要实现的目标不正确。
我建议尝试:
option.Key as option.Value for option in to.options
据我所知,您应该使用 ng-model 来设置下拉列表的默认值。
分配给 ng-model 的变量值应该包含选项的值。
例如
vm.list = [{Id : 1, Text : 'Text 1'},{Id : 2, Text : 'Text 2'}];
vm.selected = 1; // Id used as value so, set to 1 to assign it as default.
然后,
<select ng-model="vm.selected">
<option ng-repeat="option in vm.list" value="{{option.Id}}">{{option.Text}}</option>
</select>
希望对您有所帮助。