Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Reverse Checkbox State

Tags:

angularjs

Somehow, through the magic of Angular, if you use ng-model and provide it a boolean, your checkbox will be checked if said boolean is true, and unchecked if false.

<input type="checkbox" ng-model="video.hidden"> 

While this alone is fairly baffling, I'm actually trying to reverse the checked state, because unlike the todo example where todo.done means the box gets checked, my model is more like todo.incomplete.

Unfortunately my first guess didn't work:

<input type="checkbox" ng-model="!video.hidden"> 

I'm in a position where the model has been dictated to me, so I can't change it and don't want to have to massage it on the client (because I'm sending client objects back to the server, since it's running in a trusted environment).

Update

This works in 1.3 and doesn't give you strings (1.2.xxx gave you strings instead of booleans):

<input type="checkbox" ng-model="video.hidden" ng-true-value="false" ng-false-value="true"> 
like image 864
Langdon Avatar asked Dec 18 '12 02:12

Langdon


1 Answers

You can make that now without a need of custom directive, angular support ng-true-value and ng-false-value

https://docs.angularjs.org/api/ng/input/input[checkbox]

your example should work now <input type="checkbox" ng-model="video.hidden" ng-true-value="false" ng-false-value="true">

like image 64
Mahmoud Felfel Avatar answered Sep 22 '22 01:09

Mahmoud Felfel