我正在创建一个指令,用自定义标签替换普通的选择标签:
angular.module('myModule').directive('mySelect', function() {
return {
restrict: 'AE',
require: 'ngModel',
templateUrl: 'mySelect.html'
replace: true,
scope: true,
controller: ['$scope', function($scope) { $scope.options = [1,2,3,4,5] }],
link: function(scope, element, attrs, ctrl) {
// No updates to scope here
}
}
});
模板看起来像:
<select ng-model="value" ng-options="(x | sprintf:'%02d') for x in options"></select>
出于某种奇怪的原因,创建的选项ng-options
被应用了两次。生成的 HTML 如下所示:
<select my-select ng-options="(x | sprintf:'%02d') for x in options" class="...">
<option value="?" selected="selected"></option>
<option value="0">01</option>
<option value="1">02</option>
<option value="2">03</option>
<option value="3">04</option>
<option value="4">05</option>
<option value="?" selected="selected"></option>
<option value="0">01</option>
<option value="1">02</option>
<option value="2">03</option>
<option value="3">04</option>
<option value="4">05</option>
</select>
有人知道发生了什么吗?