问题描述
我有一个具有以下属性的对象 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中.默认情况下不会默认选择默认值.
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"多重>>中预先选择默认选项.使用AngularJS的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!