ng-options with disabled rows
是否可以使用
这个:
1 | <select ng-options="c.name group by c.shade for c in colors"> |
也许有可能变成这样:
1 | <select ng-options="c.name group by c.shade for c in colors | disabled(c.shade)"> |
假设通过一个过滤器,该过滤器可以为所有具有shade =" dark"的颜色返回
1 2 3 4 5 6 7 8 9 10 11 | <select> <optgroup label="dark"> <option value="0" disabled="disabled">black</option> <option value="2" disabled="disabled">red</option> <option value="3" disabled="disabled">blue</option> </optgroup> <optgroup label="light"> <option value="1">white</option> <option value="4">yellow</option> </optgroup> </select> |
@lucuma的答案(最初是可接受的答案)是正确的,但现在应该更新,因为这已在Angular 1.4中修复。请参阅ng-options的文档,其中也包含示例。
我正在使用Angular 1.5,这对我有用:
视图
控制者
{ id: 'optionA', label: 'Option A' },
{ id: 'optionB', label: 'Option B (recommended)' },
{ id: 'optionC', label: 'Option C (Later)', disabled: true }
];
vm.selectedItem = vm.items[1];
正如@Lod所指出的,Angular在1.4.0-beta.5中添加了对此的支持。
对于角度js> = 1.4.0-beta.5。
1 2 | <select ng-options="c.name disable when c.shade == 'dark' group by c.shade for c in colors"> |
对于angular js <1.4.0-beta.5,请参考以下解决方案:
与@lucuma给出的类似,但没有jQuery依赖性。
检查此http://jsfiddle.net/dZDLg/46/
控制者
1 2 3 4 | <select ng-model="selectedport" ng-options="p.name as p.name for p in ports" options-disabled="p.isinuse for p in ports"></select> <input ng-model="selectedport"> |
指示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | angular.module('myApp', []) .directive('optionsDisabled', function($parse) { var disableOptions = function(scope, attr, element, data, fnDisableIfTrue) { // refresh the disabled options in the select element. var options = element.find("option"); for(var pos= 0,index=0;pos<options.length;pos++){ var elem = angular.element(options[pos]); if(elem.val()!=""){ var locals = {}; locals[attr] = data[index]; elem.attr("disabled", fnDisableIfTrue(scope, locals)); index++; } } }; return { priority: 0, require: 'ngModel', link: function(scope, iElement, iAttrs, ctrl) { // parse expression and build array of disabled options var expElements = iAttrs.optionsDisabled.match( /^\\s*(.+)\\s+for\\s+(.+)\\s+in\\s+(.+)?\\s*/); var attrToWatch = expElements[3]; var fnDisableIfTrue = $parse(expElements[1]); scope.$watch(attrToWatch, function(newValue, oldValue) { if(newValue) disableOptions(scope, expElements[2], iElement, newValue, fnDisableIfTrue); }, true); // handle model updates properly scope.$watch(iAttrs.ngModel, function(newValue, oldValue) { var disOptions = $parse(attrToWatch)(scope); if(newValue) disableOptions(scope, expElements[2], iElement, disOptions, fnDisableIfTrue); }); } }; }); |
注意:正如每个人正确指出的,此解决方案不适用于
Angular在1.4.0-beta.5中添加了对此的支持
1 | <select ng-options="c.name disable when c.shade == 'dark' group by c.shade for c in colors"> |
我不相信有一种方法可以仅使用
https://github.com/angular/angular.js/issues/638
看来,解决方法是使用此处和github问题中引用的指令:http://jsfiddle.net/alalonde/dZDLg/9/
这是jsfiddle的全部代码供参考(以下代码来自alande的jsfiddle):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <select ng-model="selectedport" ng-options="p.name as p.name for p in ports" options-disabled="p.isinuse for p in ports"></select> <input ng-model="selectedport"> angular.module('myApp', []) .directive('optionsDisabled', function($parse) { var disableOptions = function(scope, attr, element, data, fnDisableIfTrue) { // refresh the disabled options in the select element. $("option[value!='?']", element).each(function(i, e) { var locals = {}; locals[attr] = data[i]; $(this).attr("disabled", fnDisableIfTrue(scope, locals)); }); }; return { priority: 0, require: 'ngModel', link: function(scope, iElement, iAttrs, ctrl) { // parse expression and build array of disabled options var expElements = iAttrs.optionsDisabled.match(/^\\s*(.+)\\s+for\\s+(.+)\\s+in\\s+(.+)?\\s*/); var attrToWatch = expElements[3]; var fnDisableIfTrue = $parse(expElements[1]); scope.$watch(attrToWatch, function(newValue, oldValue) { if(newValue) disableOptions(scope, expElements[2], iElement, newValue, fnDisableIfTrue); }, true); // handle model updates properly scope.$watch(iAttrs.ngModel, function(newValue, oldValue) { var disOptions = $parse(attrToWatch)(scope); if(newValue) disableOptions(scope, expElements[2], iElement, disOptions, fnDisableIfTrue); }); } }; }); function OptionsController($scope) { $scope.ports = [{name: 'http', isinuse: true}, {name: 'test', isinuse: false}]; $scope.selectedport = 'test'; } |