1

I am a beginner to AngularJS and I am making a web app that needs to disable exiting values in a dropdown list.

I know how to do it in jQuery. It is just like this: http://jsfiddle.net/qiushibaike/FcLLn/

But I don't really know how to do it in AngularJS, Should I try to disable the item or hide it? Can I set a value

HTML

<select ng-model="numbers.value" required>
     <option ng-repeat="item in items"> {{item.name}} </option>
</select><br/>

JavaScript

$scope.numbers= {};

$scope.items = [
    { id: 1, name: '11111'},
    { id: 2, name: '22222'},
    { id: 3, name: '33333'}
];

If I have something like this, how can I disable or hide the option 2 and 3 by AngularJS like what I did by using jQuery?

qiushibaike
  • 21
  • 1
  • 2
  • 3
  • 1
    suggest learning how to filter and use filter instead of disable – charlietfl Dec 01 '13 at 23:49
  • This will help you http://stackoverflow.com/questions/22005601/how-can-i-disable-to-select-the-particular-option-from-angularjs-dropdown – Harsh Feb 25 '14 at 06:16

2 Answers2

2

This is also possible with ngOptions.

You just need to add "disable when" in your ng-options tag.

In your case, you can also do like this

HTML

    <select ng-model="numbers.value" 
    ng-options="var.name as var.name disable when var.disabled for var in items" required>
      <option value="">-- Select --</option></select>

Javascript

$scope.items = [
    { id: 1, name: '11111'},
    { id: 2, name: '22222', disabled: true },
    { id: 3, name: '33333', disabled: true }
]

Plunkr

If you want to disable any option dynamically from your code,

here is a Plunkr : Dynamic Example.

0

Use Angular's ngDisabled directive.

HTML

<select ng-model="numbers.value" required>
    <option ng-repeat="item in items" ng-disabled="item.disabled"> {{item.name}} </option>
</select>

Javascript

$scope.items = [
    { id: 1, name: '11111'},
    { id: 2, name: '22222', disabled: true },
    { id: 3, name: '33333', disabled: true }
]
dutzi
  • 1,880
  • 1
  • 18
  • 23
  • As for now, there is no possibility to set disabled options with `ngOptions` directive - there is a corresponding issue https://github.com/angular/angular.js/issues/638. So you should use `ngRepeat` instead. – Alexey Dec 02 '13 at 07:13