I referred to this before asking this question.
AngularJs doesn't bind ng-checked with ng-model
If ng-checked
is evaluated to true
on the html
side, the ng-model
is not updated. I can't ng-repeat
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.
http://plnkr.co/edit/YsOsPh3vjkPMUUDa6r2t
To see what I want, please open the console, and just click on Submit
button. Please don't check any checkboxes.
Thanks in advance!
The
ng-model
andng-checked
directives should not be used togetherFrom the Docs:
Instead set the desired initial value from the controller:
You can use ng-value-true to tell angular that your ng-model is a string.
I could only get ng-true-value working if I added the extra quotes like so (as shown in the official Angular docs - https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D)
What you could do is use
ng-repeat
passing in the value of whatever you're iterating on to theng-checked
and from there utilisingng-class
to apply your styles depending on the result.I did something similar recently and it worked for me.
ngModel
andngChecked
are not meant to be used together.ngChecked
is expecting an expression, so by sayingng-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 usengTrueValue
andngFalseValue
(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 yourngModel
attributes can be simplified tong-model="testModel.item1"
.http://plnkr.co/edit/HtdOok8aieBjT5GFZOb3?p=preview
Can Declare As the in ng-init also getting true
And You Can Select the First One and Object Also Shown here true,false,flase