guleria
guleria

Reputation: 751

ng-checked not working angular for checkbox

I was trying to use checkbox and bind the checked attribute using ng-checked attribute but its not working where as its working fine with ng-model attribute with checkbox type inputs.

<!-- not working -->
<input type="checkbox" name="checkedBox"
                       id="checkedBox11"
                       ng-checked="isChecked">

<!--working-->
<input type="checkbox" name="checkedBox"
                       id="checkedBox21"
                       ng-model="isChecked">

I have created a jsbin to demonstrate the same: here

Upvotes: 7

Views: 22585

Answers (2)

Toni Ristola
Toni Ristola

Reputation: 143

Changing the first input to have model, changes it. You could also use ng-click to change the value. (addming ng-model="isChecked")

<input ng-model="isChecked" type="checkbox" name="checkedBox" id="checkedBox11" ng-checked="isChecked"> 

Or you could add ng-click="isChecked=!isChecked" to the checkbox

Upvotes: 2

TheRodeo
TheRodeo

Reputation: 465

Since you are not connecting the checkbox with a model in the first case, it is not getting changed in angular and hence the value is not changing in the view also.

However,in the second case, you have attached the isChecked to the checkbox, the changes are reflecting.

Update: If you change the default value of isChecked to true, it shows true and the checkbox is also checked on load.

Upvotes: 6

Related Questions