Alexander Burov
Alexander Burov

Reputation: 1322

angularjs: force to use array value is select

I'm relatively new to AngularJS, so maybe my question is stupid, but I cannot find an answer.

I have prepared example to explain my problem, it can be found here. When controller instantiates it has some values passed from parent scope. In my example:

$scope.modelId = 2;
$scope.sizeA = 2;   // this is initial values 
$scope.sizeB = 180; //   for select elements

Then controller requests from server other values for the lists. It loads:

"sizesA": [1, 2, 6, 9, 10],
"sizesB": [120, 180, 300]

After data is loaded, we can see initialized selects on page:

SizeA: <select ng-model="sizeA" ng-options="s for s in model.sizesA"></select>
SizeB: <select ng-model="sizeB" ng-options="s for s in model.sizesB"></select>

sizeB is initialized to value 180 (as expected), but sizeA is initialized to value 6 (with index 2) instead of value 2.

When I specify $scope.sizeA = 9; in controller (there is no index 9, but there such value) it selects item with value 9.

So the question: how can I tell angular to use value

Upvotes: 0

Views: 225

Answers (1)

New Dev
New Dev

Reputation: 49590

Add track by:

<select ng-model="sizeA" 
        ng-options="size for size in model.sizesA track by size"></select>

Upvotes: 1

Related Questions