3

I want to use a two way binding for a checkbox, but if I use the [(ngModel)] it is showing true or false instead of the value of the checked item. Does anybody know how this is done?

If i want to use a two way binding and set it to "expression.value" in my case how do i do that:

<input type="checkbox" type="checkbox"/>&nbsp;Option 1</a></li>

I would like to bind the value of the checkbox in this case: Option 1 into the expression class.

2
  • Dup of stackoverflow.com/questions/34872843/… Commented Apr 11, 2016 at 9:16
  • I updated my answer. It takes into account that expression might not yet be set when when Angular tries to resolve the binding. Commented Apr 11, 2016 at 9:36

1 Answer 1

3

This is a known issue

There are different workarounds like using event.target.checked instead of the value from the model.

You can use

<input type="checkbox"  
    (change)="expression && expression.Option1=$event.target.checked ? true : undefiend"
    [ngModel]="expression?.Option1">
<input type="checkbox"  
    (change)="expression && expression.Option2=$event.target.checked ? true : undefiend"
    [ngModel]="expression?.Option2">

Plunker example

Sign up to request clarification or add additional context in comments.

12 Comments

As you can see it is logging the event itself: i66.tinypic.com/2qd4z6t.png
I updated the answer. I mentioned it in the text but forgot to update the code.
It is logging value: "on" and not Option1 in my case
Sorry, It should be $event.target.checked
Wait i think you do not understand what my goal is. I have got multiple checkboxes Option1, option2 etc I want to bind the checked Option into my expression class. So that if i log the value of the checked items I see the label of the checked Option1, Option2
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.