如何在<select“multiple">中预选默认选项使用 AngularJS 的元素 [英] How to pre-select default options in <select "multiple"> element using AngularJS
问题描述
我有一个具有以下属性的对象 param:
I have an object param that has these properties:
param.ValidValues(键值数组)
param.DefaultValues(值字符串数组)
我需要绑定 ng-model="param.DefaultValues" 并在该选择列表框中预先选择所有默认值,以便当用户进行多项选择时,所选值被添加到 param.DefaultValues 数组
I need to bind ng-model="param.DefaultValues" and make all default values to be pre-selected in that select listbox, so that when a user makes multiple selection, the selected values are added to param.DefaultValues array
这段代码很好用,请看截图
This code does the job well, please see the screenshot
<select multiple ng-model="param.DefaultValues">
<option ng-repeat="item in param.ValidValues" value="{{item.Value}}" label="{{item.Key}}"></option>
</select>
但是,项目,这些值在 param.DefaultValues 默认情况下未预先选择.
However, the items, those values are in param.DefaultValues are not pre-selected by default.
我怎样才能做到这一点?谢谢
如果我改用 ng-options,它也不会预先选择 DefaultValues,也不会将用户选择的值添加到 DefaulValues 数组中:
If I use ng-options instead, it also does not pre-select DefaultValues, and also doesnt add user selected values to DefaulValues array:
<select ng-model="param.DefaultValues" ng-options="o.Value as o.Key for o in param.ValidValues" multiple>
<option value=""></option>
</select>
推荐答案
查看您使用哪些值来填充默认值
Take a look what values you are using to populate your default values
我使用 ng-options 因为它有更好的性能
I use ng-options because it has a better performance
检查这个例子,我几分钟前更新了.请点击此处
Check this example, I updated a few minutes ago. Please click here
我希望这对你有用.
这篇关于如何在<select“multiple">中预选默认选项使用 AngularJS 的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!