如何禁用< option> AngularJS ng-options时选择元素的选择? [英] How to disable a <option> of select element when using AngularJS ng-options?

查看:228
本文介绍了如何禁用< option> AngularJS ng-options时选择元素的选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在AngularJS中使用ng-options填充options时,是否可以禁用select元素中的option?

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

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

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

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?

推荐答案

一种方法是,忘记在select元素上使用ng-options,并在ng-repeat中添加选项,则可以在每个option上添加一个ng-disabled支票.

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={};
})

这篇关于如何禁用&lt; option&gt; AngularJS ng-options时选择元素的选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆