Javascript 在自定义指令中应用两次的Ngoption
我正在创建一个指令,用自定义标记替换普通的选择标记:Javascript 在自定义指令中应用两次的Ngoption,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我正在创建一个指令,用自定义标记替换普通的选择标记: angular.module('myModule').directive('mySelect', function() { return { restrict: 'AE', require: 'ngModel', templateUrl: 'mySelect.html' replace: true, scope: true, controller: ['$scope', function($scope)
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>
<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>
有人知道发生了什么吗?你应该使用div而不是像这样使用
<div my-select></div>
尝试在链接中定义$scope.options,而不是控制器。@ZackArgyle,在链接函数中定义选项没有影响。@bibs无法复制@sza,你的小提琴和我的例子略有不同。我调整了你的小提琴(请参见:)并注意到angular正在控制台中打印一个错误,但它没有在我的机器上打印:
错误:多个指令[select,select]要求打开“select”控制器:
。将指令作为元素而不是现有对象上的属性使用可以解决此问题。
<select ng-options="x for x in options" ng-model="value" my-select=""></select>