John Doe
John Doe

Reputation: 635

ngOptions non-strict comparison with ngModel

Is there any way to perform a non-strict comparison in the <select> ngOptions in order to set the initial value from a model? For example, if the model is an integer and the options key property is a string value, the <select> initial value unfortunately is not set.

HTML:

<div ng-app="app">
  <div ng-controller="Controller">
    <select data-ng-model="model" data-ng-options="option.id as option.text for option in options"></select>
  </div>
</div>

JS:

var app = angular.module('app', []);

app.controller('Controller', function ($scope) {
    $scope.model = '2'; // This works as an integer (2) but not as a string ('2')
    $scope.options = [
      {id: 1, text: '11111'},
      {id: 2, text: '22222'}, // I want this option to be selected
      {id: 3, text: '33333'}
    ];
});

Upvotes: 0

Views: 120

Answers (1)

ThibaudL
ThibaudL

Reputation: 1009

If I'm not mistaken you need to add the "track by" keywords in your ng-options, such as :

    <select data-ng-model="model" data-ng-options="option.id as option.text for option in options track by option.id"></select>

Upvotes: 0

Related Questions