Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean Filter for checkboxes in ng-model Angular

item.imposed is either 1, or 0.

In order for a checkbox to mark as checked, the value must be true or false.

<input type="checkbox" ng-model="item.imposed">

You cannot use a filter inside of ng-model, how can this be accomplished simply and correctly?

like image 742
TaylorMac Avatar asked May 17 '13 00:05

TaylorMac


2 Answers

You can use ngChecked, but you won't get any binding back to your model:

http://jsfiddle.net/fMBQj/

Or you can use ngTrueValue and ngFalseValue, but you HAVE to use a string (not an int):

http://jsfiddle.net/fMBQj/1/

Or you can use a custom directive... it's pretty lame right now.

like image 149
Langdon Avatar answered Nov 09 '22 15:11

Langdon


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)

ng-true-value="'1'"
like image 41
JRT Avatar answered Nov 09 '22 15:11

JRT