Search code examples
angularjsjsonng-options

Track option in select based on a specific property


I am diving directly on my problem:

On controller:

$scope.code = 1
$scope.codes [
  {
    "1":"Accepted"
    "0":"Rejected"
  }

On view:

<select ng-model="code" ng-options="key as value for (key,value) in codes track by key"></select>

Is there any way to have "Accepted" selected on my view ? I know that the $scope.code is not an object but I want to assign it the value passed by selected option (key).

For anyone who would like to reproduce my problem, I got a plunk made.


Solution

  • You are almost there, you just need to do minor changes:

    Remove the track by statement like this:

    <select ng-model="myVal" ng-options="key as val for (key,val) in data"></select>

    And in $scope.myVal set the value as string because you are assigning an integer.

    And that should work.