简体   繁体   English

如何在中预先选择默认选项 <select “multiple”>使用AngularJS的元素

[英]How to pre-select default options in <select “multiple”> element using AngularJS

I have an object param that has these properties: 我有一个具有以下属性的对象参数

param.ValidValues (key-value array) param.ValidValues(键值数组)

param.DefaultValues (values string array) 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 我需要绑定ng-model =“ param.DefaultValues”并在该选择列表框中预先选择所有默认值,以便在用户进行多项选择时,将所选值添加到param.DefaultValues数组中

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>

在此处输入图片说明 However, the items, those values are in param.DefaultValues are not pre-selected by default . 但是,这些值都在param 默认情况下不会默认选择 DefaultValues。

How can I achive that? 我该如何实现? Thanks 谢谢

If I use ng-options instead, it also does not pre-select DefaultValues, and also doesnt add user selected values to DefaulValues array: 如果我改用ng-options,它也不会预先选择DefaultValues,也不会将用户选择的值添加到DefaulValues数组中:

<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 看看您使用什么值来填充默认值

I use ng-options because it has a better performance 我使用ng-options是因为它具有更好的性能

Check this example, I updated a few minutes ago. 检查此示例,我在几分钟前进行了更新。 Please click here 请点击这里

I hope this will be useful for you. 希望对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM