Eike Thies
Eike Thies

Reputation: 1319

How to preselect ng-options based on an array value?

So this is kind of tricky - i have an array of possible values for a ng-options select input. Now the actual value of a entry might be an array itself (don't ask why, its just my requirement) - everything works (array is passed along and can be used) but what does NOT work is that the select list is preselected with the corresponding label if the value(model) is already the mentioned array.

Okay just look at this simple example and you know what i mean

http://jsfiddle.net/DTn62/

$scope.testArray = [{label:"to be reviewed",value:["acceptedByModerator","REVIEW_LATER"]},
                        {label:"public",value:'acceptedByModerator'},
                        {label:"not public",value:"rejectedByModerator"},
                        {label:"scheduled for later",value:"REVIEW_LATER"}];
$scope.mytest = ["acceptedByModerator","REVIEW_LATER"]; //<-- Does not preselect select field
//$scope.mytest = "acceptedByModerator"; //<-- Works, Does preselect field

does anybody know a solution to this or is this a bug? I know the easiest solution would be to just not use an array as a value, but unfortunately this would lead to more ugly if/else code later on...

Upvotes: 0

Views: 474

Answers (1)

mpm
mpm

Reputation: 20155

replacing

 $scope.mytest = ["acceptedByModerator","REVIEW_LATER"];

by

   $scope.mytest = $scope.testArray[0].value

works :

http://jsfiddle.net/camus/C6Qn6/

see if doing that can meet your requirements. ultimatly angular is testing for some reference equality (===) i suppose.

so you need to keep that in mind when comparing the default value to the option value.It's not a problem with strings,or numbers,but with objects yes.

edit :

dont forget that angular adds $$ properties to objects in $scope too.

Upvotes: 1

Related Questions