这个例子看起来很简单,我似乎无法弄清楚它为什么不起作用(我不想使用 ng-options,因为它不适用于 select2,这是我得到这个后想使用的插件想通了):
HTML:
<div ng-app="myApp">
<div ng-controller="MyCtrl">
selectedNumber: {{selectedNumber}}
<select ng-model="selectedNumber">
<option ng-repeat="number in numbers" value="{{number}}">{{number}}</option>
</select>
<div ng-repeat="number in numbers">
{{number}}
</div>
</div>
</div>
AngularJS:
var app = angular.module('myApp', []);
app.controller('MyCtrl', function($scope) {
$scope.numbers = [1, 2];
$scope.selectedNumber = 2;
});
检查元素时,它看起来像这样:
<select ng-model="selectedNumber" class="ng-pristine ng-valid">
<option value="? number:2 ?"></option>
<!-- ngRepeat: number in numbers -->
<option ng-repeat="number in numbers" value="1" class="ng-scope ng-binding">1</option>
<option ng-repeat="number in numbers" value="2" class="ng-scope ng-binding">2</option>
</select>
我猜是额外的“ <option value="? number:2 ?"></option>
”导致了这个问题,但我不知道如何摆脱它。我还在行动中创建了一个jsfiddle。