FarazShuja
FarazShuja

Reputation: 2370

how to update dropdown value dynamically

I have html code like this

<select id="userGroups" name="userGroups" ng-model="userGroups" required class="form-control">
    <option value="{{grp.groupId}}" ng-repeat="grp in groups">{{grp.groupName}}</option>
</select>

and in my controller I want to set the default value but its not working

function CreateUserController($scope, grpList) {
    $scope.groups = grpList.groupList; //it is loading correctly and dropdown is populating correctly
    $scope.userGroups = "2";
}

I am trying to set the userGroups value to 2, but its always showing the first option in select

Upvotes: 0

Views: 340

Answers (1)

Gruff Bunny
Gruff Bunny

Reputation: 27976

The best bet here is to use ng-options:

<select ng-model="userGroups" 
        ng-options="group.groupId as group.groupName for group in groups">
</select>

Fiddle

Upvotes: 1

Related Questions