简体   繁体   中英

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.DefaultValues (values string array)

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>

在此处输入图片说明 However, the items, those values are in param.DefaultValues are not pre-selected by default .

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:

<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

Check this example, I updated a few minutes ago. Please click here

I hope this will be useful for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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