How can I put multiple values for ng-disabled in angular js?
My problem is explained with this following JS fiddle: 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.
I hope, you will get my question.
Thank you !!
Use two seperate variables i.e checkA and checkB. Then for ng-disabled use the logical OR operator like this:
ng-disabled="checkA || checkB"
I think this is what you are trying to achieve...?
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)
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 :
<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 :
$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 :
<input type="checkbox" ng-disabled="isThisDisabled()">Chkbox1 to be disabled</input>
The modified fiddle to test this : http://jsfiddle.net/DotDotDot/FJf4v/12/
Have fun
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With