10

当我们在 AngularJS 中填充 ng-options时,有没有办法禁用元素option中的an?selectoptions

像这样:http ://www.w3schools.com/tags/att_option_disabled.asp

您可以通过添加ng-disabled到每个<option>标签来做到这一点,但是如果我们ng-options在标签中使用指令,有没有办法做到这一点select

4

5 回答 5

16

一种方法是,忘记ng-optionsselect元素上使用,并在 with 中添加选项ng-repeat,然后您可以ng-disabled在每个option.

http://jsfiddle.net/97LP2/

<div ng-app="myapp">
    <form ng-controller="ctrl">
        <select id="t1" ng-model="curval">
            <option ng-repeat="i in options" value="{{i}}" ng-disabled="disabled[i]">{{i}}</option>
        </select>
        <button ng-click="disabled[curval]=true">disable</button>
    </form>
</div>

用于测试的最小控制器:

angular.module('myapp',[]).controller("ctrl", function($scope){
    $scope.options=['test1','test2','test3','test4','test5'];
    $scope.disabled={};
})
于 2014-01-08T18:31:51.593 回答
16

Angular 1.4开始:我们可以disable when在 ng-options 中使用,例如:

 $scope.colors = [
  {name:'black', shade:'dark'},
  {name:'white', shade:'light', notAnOption: true},
  {name:'red', shade:'dark'},
  {name:'blue', shade:'dark', notAnOption: true},
  {name:'yellow', shade:'light', notAnOption: false}
];
$scope.myColor = $scope.colors[2]; // red

此示例具有按阴影分组的颜色,其中一些已禁用:

<select ng-model="myColor" ng-options="color.name group by color.shade disable when color.notAnOption for color in colors"></select>

我从angular 文档中得到了这段代码。

于 2016-01-26T02:52:34.457 回答
3

您应该使用ng-disabled指令http://docs.angularjs.org/api/ng.directive:ngDisabled

<body ng-app="myApp">

  <div ng-controller="myCtrl">
    <form class="form-inline">
      <div class="form-group">
        <select class="form-control" ng-disabled="disableSelect">
          <option val="one">First</option>
          <option val="two">Second</option>
        </select>
      </div>

      <button class="btn btn-default" ng-click="disableSelect = !disableSelect">
        Disable select
      </button>
    </form>
  </div>

</body>

在这里您可以找到完整的示例http://jsbin.com/ihOYurO/1/

于 2014-01-08T17:52:20.853 回答
2

Solve with version 1.4 https://docs.angularjs.org/api/ng/directive/ngOptions [double check you look for v1.4 documentation]

于 2015-03-28T00:19:06.733 回答
1

大概是这样的

<button ng-click='disableSelect=true'></button>

<select ng-disabled='disableSelect'></select>
于 2014-01-08T17:48:37.550 回答