20

我有一个 html 选择选项

<select>
    <option ng-repeat="field in filter.fields" value="{{field.id}}">{{field.name}}</option>
</select>

我正在从 ng-repeat 迭代,我想根据一个可选的文件禁用选项,例如

<select>
    <option ng-repeat="field in filter.fields" {field.selectable==true?enable:disable} value="{{field.id}}">{{field.name}}</option>
 </select>

我怎样才能用角度来实现这一点?

4

3 回答 3

27

假设你有这样的结构:

  $scope.filter = {
    fields: [
      {id: 1, name: "a", selectable: false},
      {id: 2, name: "asdf", selectable: true},
      {id: 3, name: "qwet", selectable: false},
      {id: 4, name: "qnjew", selectable: true},
      {id: 5, name: "asdjf", selectable: false}
    ]
  };

这应该适合你:

  <select>
    <option ng-repeat="field in filter.fields" ng-disabled="field.selectable" value="{{field.id}}">{{field.name}}</option>
  </select>
于 2013-09-10T15:26:12.080 回答
11

虽然 ng-disabled 属性在技术上会起作用,但在选项上使用 ng-repeat 时可能会遇到错误。这是一个众所周知的问题,也正是 Angular 团队创建 ng-options 的原因。还没有将 ng-options 和 ng-disabled 一起使用的角度实现,但是Alec LaLonde创建了这个自定义指令,您可以添加和使用它。请参阅此处的问题论坛:https://github.com/angular/angular.js/issues/638和该帖子中的 jsfiddle

angular.module('myApp', [])
.directive('optionsDisabled', [ '$parse', function($parse) {
        var disableOptions = function($scope, attr, $element, data, fnDisableIfTrue) {
            $element.find('option:not([value="?"])').each(function(i, e) { //1
                var locals = {};
                locals[attr] = data[i];
                $(this).attr('disabled', fnDisableIfTrue($scope, locals));
            });
        };

        return {
            priority: 0,
            require: 'ngModel',
            link: function($scope, $element, attributes) { //2
                var expElements = attributes.optionsDisabled.match(/^\s*(.+)\s+for\s+(.+)\s+in\s+(.+)?\s*/),
                    attrToWatch = expElements[3],
                    fnDisableIfTrue = $parse(expElements[1]);
                $scope.$watch(attrToWatch, function(newValue, oldValue) {
                    if (!newValue) return;

                    disableOptions($scope, expElements[2], $element, newValue, fnDisableIfTrue);
                }, true);

                $scope.$watch(attributes.ngModel, function(newValue, oldValue) { //3
                    var disabledOptions = $parse(attrToWatch)($scope);
                    if (!newValue) return;

                    disableOptions($scope, expElements[2], $element, disabledOptions, fnDisableIfTrue);
                });
            }
        };
    }
]);
//1 refresh the disabled options in the select element
//2 parse expression and build array of disabled options
//3 handle model updates properly

function OptionsController($scope) {
    $scope.ports = [{name: 'http', isinuse: true},
                    {name: 'test', isinuse: false}];

    $scope.selectedport = 'test';
}
于 2013-09-10T15:31:42.677 回答
4

这实际上是一个相当古老的问题。在更高版本的 Angular(角度 1.4+)中,您有 ngOptions 指令。链接在这里:-

https://docs.angularjs.org/api/ng/directive/ngOptions

现在有一种处理这种情况的语法:-

label disable when disable for value in array track by trackexpr

我想我会放这个以防其他人访问此页面。

于 2016-06-08T05:10:01.597 回答