TaylorMac
TaylorMac

Reputation: 9002

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?

Upvotes: 5

Views: 3852

Answers (2)

BlueberrySourRaspberry
BlueberrySourRaspberry

Reputation: 963

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'"

Upvotes: 3

Langdon
Langdon

Reputation: 20063

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.

Upvotes: 11

Related Questions