0

I have the following select list:

<select multiple="multiple" ng-model="userRoles" ng-options="r.id as r.name for r in roles"></select>

The above produces a list of all the available roles. I need to preselect the roles that the user is a member of when presenting the form. I can assign a role to userRoles, but if I try create an array and push the roles associated with the user into that array, none of the items are selected.

I've created a jsBin to let you play with the non-working solution (and hopefully fix it): http://jsbin.com/icoSoj/1/edit?html,js,output

Thanks!

2 Answers 2

1

Your selected values are IDs and not full objects.

As you do r.id as r.name, you need to put IDs into your array and not the full objects.

http://jsbin.com/uFiDuxa/1/edit

Sign up to request clarification or add additional context in comments.

1 Comment

Silly me - I only added the r.id as later to try solve the problem and made a new one for myself. doh
1

Inside ng-options you are specifying ids as values for the options. So you should do:

$scope.userRoles = [
    $scope.roles[1].id,
    $scope.roles[2].id
];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.