Sasa
Sasa

Reputation: 563

angularjs checked box in form

I want to when edit user to fill all field in form automatically. I managed to fill input and select fields, but don't know how to do that with checkbox, to be checked if they need to be checked. As a matter of fact I did but after a few days that solution stopped working even nothing has changed. I wrote my code in this way: html

<label><input type="checkbox" value="1" name="admin" ng-model="userDataCtr.user.admin"  ng-checked="userDataCtr.user.admin">ROLE_CRO</label>

And value of userDataCtr.user.admin is 1, but checkbox isn't checked. I tried with integer, string, but nothing happened.

Does anyone can help me?

Thanks

Upvotes: 1

Views: 44

Answers (1)

kukkuz
kukkuz

Reputation: 42360

The value of a checkbox is boolean- so userDataCtr.user.admin should be either true or false.

<label><input type="checkbox" name="admin" 
   ng-model="userDataCtr.user.admin">ROLE_CRO</label>

Reference: https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D

EDIT As @Harris Weinstein points out in the comment to this answer below, you should not use ng-checked and ng-model together.

Reference: https://docs.angularjs.org/api/ng/directive/ngChecked

Upvotes: 1

Related Questions