How to bind ng-model and checkbox values

13,725 views
Skip to first unread message

Antonello Pasella

unread,
Mar 18, 2013, 7:18:18 AM3/18/13
to ang...@googlegroups.com
Hi, I have a property that (from my JSON) looks like

{
    property : "1" // or property : "0"
}


How can I have in my model "0" or "1" instead of true/false?

With my JSON data this declaration

{{outlet}} Outlet <input type="checkbox" ng-model='property'/> become 

0 Outlet #unchecked input#

If I check the checkbox it become

true Outlet #checked input#

If I check it again it become

true Outlet #checked input#

Is there a way to define two values to use with true/false?

Thanks!

Antonello

Pawel Kozlowski

unread,
Mar 18, 2013, 7:20:57 AM3/18/13
to ang...@googlegroups.com
Hi!

Have a look at the ngTrueValue / ngFalseValue:
http://docs.angularjs.org/api/ng.directive:input.checkbox

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to angular+u...@googlegroups.com.
> To post to this group, send email to ang...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Looking for bootstrap-based widget library for AngularJS?
http://angular-ui.github.com/bootstrap/

Antonello Pasella

unread,
Mar 18, 2013, 7:31:44 AM3/18/13
to ang...@googlegroups.com
Woops!!!

Thanks!


Il giorno lunedì 18 marzo 2013 12:20:57 UTC+1, Pawel Kozlowski ha scritto:
Hi!

Have a look at the ngTrueValue / ngFalseValue:
http://docs.angularjs.org/api/ng.directive:input.checkbox

Cheers,
Pawel

Federico Scarabino

unread,
May 6, 2013, 1:55:53 PM5/6/13
to ang...@googlegroups.com
Hi guys,
What about this one, 

I can not get the checkboxes to take the value specified on ng-true-value or ng-false-value.

Any thoughts?
Thanks in advance!

Luke Kende

unread,
May 7, 2013, 3:35:07 AM5/7/13
to ang...@googlegroups.com
To save some headache on this problem, I run json data through conversion.  Instead of using ng-true-value and ng-false-value, which still wouldn't get it right for me on string integers, I found it easier to convert to a boolean any data from the server...  

Lars Rye Jeppesen

unread,
Nov 8, 2013, 6:39:12 AM11/8/13
to ang...@googlegroups.com
The problem with ng-true-value and ng-false-value is that they only take STRING values.

If you try to bind to a model that is an integer (for example 0 or 1) then you're in trouble.
Reply all
Reply to author
Forward
0 new messages