2

I wanted to set selected option in my dropdown list.

I have two ways of passing data into selects ng-model

first: $scope.selectedTeam = $scope.teams[31];

second: $scope.selectedTeam = selectedTeamSrvs.getTeam()

my html select looks like this:

<select ng-model="selectedTeam" 
    ng-options="team as team.name for team in teams | orderBy:'ID'">
</select>

and JSON.stringify($scope.teams[31]) === JSON.stringify(selectedTeamSrvs.getTeam()) gives true. Then why only first option sets selected option correctly?

2
  • that what you need to do... $scope.selectedTeam = JSON.stringify(selectedTeamSrvs.getTeam()); Commented Apr 26, 2015 at 13:51
  • Sorry bad paste, stand corrected. Commented Apr 26, 2015 at 13:55

1 Answer 1

2

By default, ngModel compares by reference, not value.This is important when binding to an array of objects

Second option is not setting correctly because selectedTeamSrvs.getTeam() returns a different instance of team, even though they are equal when stringified.

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

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.