最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable dropdown list item in AngularJS? - Stack Overflow

programmeradmin2浏览0评论

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: /

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?

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/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?

Share Improve this question asked Dec 1, 2013 at 23:44 qiushibaikeqiushibaike 211 gold badge2 silver badges3 bronze badges 2
  • 1 suggest learning how to filter and use filter instead of disable – charlietfl Commented Dec 1, 2013 at 23:49
  • This will help you stackoverflow./questions/22005601/… – Harsh Commented Feb 25, 2014 at 6:16
Add a ment  | 

2 Answers 2

Reset to default 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.

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 }
]
发布评论

评论列表(0)

  1. 暂无评论