0

有什么方法可以执行非严格比较<select> ngOptions以便从模型中设置初始值?例如,如果模型是整数并且选项键属性是字符串值,那么<select>很遗憾没有设置初始值。

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'}
    ];
});
4

1 回答 1

0

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>
于 2018-04-22T13:27:55.743 回答