Abilash Abilash - 1 year ago 128
AngularJS Question

AngularJS: ng-model not binding to ng-checked for checkboxes

I referred to this before asking this question.

AngularJs doesn't bind ng-checked with ng-model


is evaluated to
on the
side, the
is not updated. I can't
as suggested in the above question because I have to use some styling for each checkbox.

Here is the plunker that I have created to illustrate my problem.


To see what I want, please open the console, and just click on
button. Please don't check any checkboxes.

Thanks in advance!

Answer Source

ngModel and ngChecked are not meant to be used together.

ngChecked is expecting an expression, so by saying ng-checked="true", you're basically saying that the checkbox will always be checked by default.

You should be able to just use ngModel, tied to a boolean property on your model. If you want something else, then you either need to use ngTrueValue and ngFalseValue (which only support strings right now), or write your own directive.

What is it exactly that you're trying to do? If you just want the first checkbox to be checked by default, you should change your model -- item1: true,.

Edit: You don't have to submit your form to debug the current state of the model, btw, you can just dump {{testModel}} into your HTML (or <pre>{{testModel|json}}</pre>). Also your ngModel attributes can be simplified to ng-model="testModel.item1".


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download