Html 如何在 angular js 中为 ng-disabled 设置多个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18261685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I put multiple values for ng-disabled in angular js?
提问by Rakmo
How can I put multiple values for ng-disabled in angular js?
如何在 angular js 中为 ng-disabled 设置多个值?
My problem is explained with this following JS fiddle: http://jsfiddle.net/FJf4v/10/
我的问题用以下 JS 小提琴解释:http: //jsfiddle.net/FJf4v/10/
<div ng-app>
<div ng-controller="myCnt">
<h3>A ->> <input type="checkbox" ng-model="check"> </input></h3>
<h3>B ->> <input type="checkbox" ng-model="check"> </input></h3>
<br/>
<input type="checkbox" ng-disabled="check">Chkbox1 to be disabled</input>
<input type="checkbox" ng-disabled="check">Chkbox2 to be disabled</input>
<hr/>
</div>
</div>
Javascript:
function myCnt($scope) {
//
}
In this fiddle, there are total 4 check boxes labeled as A, B, Chkbox1 and Chkbox2. What I am looking for is to disable both chkbox1 and chkbox2 after checking one of A and B checkboxes. However, its done halfway. If you click on either A or B, both these buttons are getting checked and below chkboxes are getting disabled. But, I dont want to get checked both A and B checkboxes if I just click on any one of them.
在这个小提琴中,共有 4 个复选框标记为 A、B、Chkbox1 和 Chkbox2。我正在寻找的是在选中 A 和 B 复选框之一后禁用 chkbox1 和 chkbox2。然而,它完成了一半。如果您单击 A 或 B,这两个按钮都会被选中,并且下面的 chkbox 会被禁用。但是,如果我只是单击其中任何一个,我不想同时选中 A 和 B 复选框。
I hope, you will get my question.
我希望,你会得到我的问题。
Thank you !!
谢谢 !!
回答by Drew R
Use two seperate variables i.e checkA and checkB. Then for ng-disabled use the logical OR operator like this:
使用两个单独的变量,即 checkA 和 checkB。然后对于 ng-disabled 使用逻辑 OR 运算符,如下所示:
ng-disabled="checkA || checkB"
I think this is what you are trying to achieve...?
我认为这就是你想要达到的目标......?
回答by DotDotDot
So, as I said in the comments, if you want to achieve this with a very long list of checkboxes, you can try this way : (yeah for this example "very long" means 10)
所以,正如我在评论中所说,如果你想用很长的复选框列表来实现这一点,你可以尝试这种方式:(是的,对于这个例子,“很长”意味着 10)
The models
You can use a simple object which will contain all the values of the checkboxes, I did this using a ng-repeat, but it could be anything, just use the same iterable object :
模型
您可以使用一个简单的对象,该对象将包含复选框的所有值,我使用 ng-repeat 做到了这一点,但它可以是任何东西,只需使用相同的可迭代对象:
<input type="checkbox" ng-repeat="nb in [1,2,3,4,5,6,7,8,9,10]" ng-model="checkModels[nb]" /></h3>
This object is initialized in the controller, I didn't specify any value, but you could for example set some checkboxes here :
这个对象在控制器中初始化,我没有指定任何值,但你可以在这里设置一些复选框:
$scope.checkModels={};
The disabling function
Nothing very difficult here, instead of a long list of || values, we will iterate through the models and return true as soon as we find a checked box :
禁用功能
这里没什么难的,代替一长串 || 值,我们将遍历模型并在找到复选框后立即返回 true :
$scope.isThisDisabled=function(){
for(var i in $scope.checkModels)
{
if($scope.checkModels[i])
return true
}
return false;
}
Then, we can use this function in the HTML :
然后,我们可以在 HTML 中使用这个函数:
<input type="checkbox" ng-disabled="isThisDisabled()">Chkbox1 to be disabled</input>
The modified fiddle to test this : http://jsfiddle.net/DotDotDot/FJf4v/12/
修改后的小提琴来测试这个:http: //jsfiddle.net/DotDotDot/FJf4v/12/
Have fun
玩得开心