W
W
WebDev2015-04-20 18:23:22
Angular
WebDev, 2015-04-20 18:23:22

Angular ng-options how to set selected?

Please tell me, there is an array of accounts, I display them like this:

<select class="js-example-basic-multiple" 
                                ng-model="new_user_accounts" 
                                ng-options="account.title for account in accounts track by account.id"
                                multiple="multiple">
                        </select>

And there is an array of selected accounts (id's). How do I change ng-options to check for account.id in selected accounts and add selected?
Without using ng-options, I did this:
<option ng-repeat="account in accounts" 
                            ng-selected="user_accounts.indexOf(account.id) > -1"
                            value="{{account.id}}">{{account.title}}</option>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kompi, 2015-04-21
@kirill-93

If I understand correctly, then this is necessary for the initial setting of values ​​​​for select according to a certain condition.
If so, then it can be done in a controller/directive.
Example .

T
Timofey, 2015-04-20
@mr_T

I don't know if this will work for a multiple value, but there's definitely a problem with the ng-options attribute value being mistyped, since the actual value passed to (and read from) ng-model will be the full account object, not account. id. You need to use select as label for value in array , where select => account.id, label => account.title, value => account. In general, the question would disappear by itself if you looked here . There is exactly what I wrote in the table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question