10

Is there a way to disable an option in a select element when we populate options with ng-options in AngularJS?

Like this: http://www.w3schools.com/tags/att_option_disabled.asp

You can do it by adding ng-disabled to each <option> tag, but is there a way to do it if we are using the ng-options directive in the select tag?

Asim K T
  • 16,864
  • 10
  • 77
  • 99
badaboum
  • 843
  • 2
  • 17
  • 28

5 Answers5

17

One way to do this is, to forget using ng-options on the select element, and add the options in with ng-repeat, then you can add a ng-disabled check on each 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>

a minimal controller for testing:

angular.module('myapp',[]).controller("ctrl", function($scope){
    $scope.options=['test1','test2','test3','test4','test5'];
    $scope.disabled={};
})
DeadAlready
  • 2,988
  • 19
  • 18
towr
  • 4,147
  • 3
  • 20
  • 30
  • Yea I agree with @towr, I don't see a solution using ng-options in the select tag. I like this implementation it's clean and easy to understand. – NicolasMoise Jan 08 '14 at 18:38
  • 2
    It looks like Angular 1.4's [ngOptions](https://code.angularjs.org/1.4.0-beta.5/docs/api/ng/directive/ngOptions) will allow `label disable when disable for ...` – Pakman Mar 13 '15 at 20:56
  • @towr the solution works well for single dropdown, but what if we have multiple dropdowns populated from same single json? As in my case the option selected in one dropdown is disabled but not reflected in other dropdowns... – Diksha Feb 28 '19 at 10:09
  • I think it should work if the lists use the same scope, so that might be the issue. One option is to put the options-list in the `$root` (probably not the nicest way to do it): http://jsfiddle.net/2dsoh7xq/ – towr Feb 28 '19 at 16:00
16

From Angular 1.4 : We can use disable when in ng-options, for example:

 $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

This example have colors grouped by shade, with some disabled:

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

I got this code from the angular documentation.

Asim K T
  • 16,864
  • 10
  • 77
  • 99
Nicolas Del Valle
  • 1,384
  • 15
  • 20
  • 2
    This is the best solution, but one thing worth pointing out is that it requires Angular 1.4 or above as @Fulup mentions. :) – Backer Jun 07 '16 at 09:18
3

You should use ng-disabled directive 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>

Here you could find the complete example http://jsbin.com/ihOYurO/1/

luacassus
  • 6,540
  • 2
  • 40
  • 58
  • cool example i had the same in my application but what i want is to disable the options inside the select and not the select ? Capiché ? – badaboum Jan 08 '14 at 18:11
  • @badaboum yea that wasn't very clear at all in your question, write better questions and you'll get better answers. – NicolasMoise Jan 08 '14 at 18:19
  • @NicolasMoise i'm not a native English speaker ! peace – badaboum Jan 08 '14 at 18:28
  • 2
    @badaboum no problem. I think what you actually meant to say was a great question. I just want other people to be able to understand it and link to it in the future as well. It's kind of the point of this website. – NicolasMoise Jan 08 '14 at 18:31
2

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

Fulup
  • 545
  • 3
  • 14
1

Probably something like this

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

<select ng-disabled='disableSelect'></select>
NicolasMoise
  • 7,261
  • 10
  • 44
  • 65
  • You sure? @luacassus's jsbin example seems to work and he uses the same logic – NicolasMoise Jan 08 '14 at 18:02
  • @NicolasMoise yes it si working absolutly well but i need to disable the option and not the select ? any idea ? – badaboum Jan 08 '14 at 18:08
  • @NicolasMoise need to edit the – badaboum Jan 08 '14 at 18:09
  • Well this is very easy if you use ng-options instead of manually typing out the option tags.In your controller, you'll have an array containing the options of your select (their value, label, etc...) then just write a function that removes elements from that array and call it when you press the button. – NicolasMoise Jan 08 '14 at 18:13
  • @NicolasMoise Ah, I made a typo in my jsfiddle myself which caused it to fail, lol, that's ironic. – towr Jan 08 '14 at 18:14