pleerock pleerock - 9 months ago 39
Javascript Question

AngularJS - multiselect options when model and options are different

I have multiple select box for user to select his roles.

<select ng-options=" for role in roles"

"roles" - is the list of roles that user can select


"user.roles" - list of roles that user actually have


Both "roles" and "user" are loading dynamically via ajax.
The problem is that when I load user that already have roles - they are not automatically selected in the select box. How do I select them?


What is your version of angular ?

In angular 1.0.X, angular check if items are strictly equals (so roles must be the same instance in ng-model and selected item in array).

If you use a more recent version of angular (1.2.x), you can use 'track by' in your ng-option:

    ng-options=" for role in roles track by"

See this fiddle: