我想要一個指令,它把ngrange取代爲[ - 3,3]到ng-options。角度不正確的編譯選項
不幸的是下面的解決方案是不好
<html lang="pl">
<head>
<title>Its directive range</title>
</head>
<body>
<div ng-app="app">
<div ng-controller="AppCtrl">
<select ng-model="value" range="[-3, 3]"></select>
<span>{{value}}</span>
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script type="text/javascript">
angular.module('directives', []);
angular.module('app', ['directives']);
angular.module('app').controller('AppCtrl', function($scope) {
$scope.value = 2;
});
angular.module('directives').directive('range', function($compile) {
return {
restrict: 'A',
scope: {
range: '='
},
link: function(scope, elem, attr) {
scope.options = [];
for(var i = scope.range[0]; i <= scope.range[1]; ++i) {
scope.options.push(i);
}
elem.attr('ng-options', 'o as o for o in options track by o');
elem.removeAttr('range');
$compile(elem)(scope);
}
};
});
</script>
</body>
後創建NG選項標籤選擇包含期權價值= 「號:2」,而不是選擇值2 ...
我創建plunker你的代碼,但不能複製上述問題,呈現的HTML是: – Andriy
plunker:http://plnkr.co/edit/W0Uz9t4bnGh6feVqBYP4?p=preview – Andriy
我在控制器中設置$ scope.value = 2,但它沒有選擇選項...並創建了值爲「number:2」的選項... – smiady