Ad

Angular Checkbox First Click Does Nothing If Ng-checked Is True

- 1 answer

So, this seems like it must be a newb question. Here is a plunkr http://plnkr.co/edit/r9xWht5DZtOTvbblXwYZ

<input id="reviews"  ng-model="reviews" type="checkbox" ng-checked="reviews == 1" />

Basically I have a boolean in the db that I set scope to, it is either 1 or 0 in db. If it is true then the checkbox is checked, if not then it isn't. The problem I am having is that if it is checked, the first time 1 uncheck the box, scope doesn't change. When I click it (check it again) the scope value turns into true, then it works as expected.

Again, I must be overlooking something trivial here. I was looking into $scope.$watch should I be using that?

Thanks!

Ad

Answer

Instead of ng-checked you can use ng-true-value and ng-false-value instead.

See plunker

<input id="reviews"  ng-model="reviews" type="checkbox" ng-true-value="1" ng-false-value="0" />
Ad
source: stackoverflow.com
Ad