1

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

You should add a reference to the ng-checked documentation, which specifically states that it should not be used with ng-model.
I know you didn't; Sasa did. Your point about using a boolean is fine (though a 1 could be interpreted as truthy by Angular), but I think your answer will help Sasa more if you point out the active hindrance of the conflicting directives that's warned against in the documentation.
If I just type 1 in ng-checked checkbox is checked, but if I send number 1 as value (userDataCtr.user.admin) then isn't checked
@Sasa I suggest you take out value and ng-checked entirely, and only use ng-model. It's all you need, and the other attributes potentially clash with it.
I changed value to true and false and now works. I was trying that a couple of times and didn't but now somehow does work. I am confused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.