WebDev2015-04-20 18:23:22
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-options="account.title for account in accounts track by account.id"

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"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
kompi, 2015-04-21

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 .

Timofey, 2015-04-20

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