Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  16] [ 7]  / answers: 1 / hits: 22874  / 11 Years ago, thu, august 15, 2013, 12:00:00

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 !!


More From » html

 Answers
4

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


[#76328] Wednesday, August 14, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;