JimShelley
JimShelley

Reputation: 133

Set Value of Angular Dropdownlist on update form

We have an simple update User form. One of the values people will be able to update is their ROLE from a dropdownlist. The options for Roles come from a lookup table and each user will already have a role assigned at the time of updating.

When I try to bind to their current Role in my input, I am unable to set the select value to the one in the currently selected User model.

Here is my dropdown:

<select ng-model="SelectedUser.Role" 
       ng-options="role.Id as role.Name for role in Roles">
  <option value="">-- Please Select A Role --</option>
</select><br />   

My models are this:

$scope.GetSelectedUser = function () {
            var userId = $scope.SelectedUser.UserId;
        }

$scope.SelectedUser = {};

$scope.Roles = Role.query();

Upvotes: 0

Views: 220

Answers (2)

Charles
Charles

Reputation: 132

If you're calling an angular $resource to get your SelectedUser like you are for Roles, first, make sure that your service is returning the Role.Id in the "Role" property. If the property returned from the service doesn't match your list, it will look like it just isn't hooked up.

Upvotes: 1

smackenzie
smackenzie

Reputation: 3022

try adding a track by

<select ng-model="SelectedUser.Role" 
       ng-options="role.Id as role.Name for role in Roles track by role.id">
  <option value="">-- Please Select A Role --</option>
</select><br />   

don't you want to work with objects rather than IDs?

i.e.

<select ng-model="SelectedUser.Role" 
       ng-options="role.Name as role for role in Roles track by role.id">
  <option value="">-- Please Select A Role --</option>
</select><br />   

Upvotes: 0

Related Questions