2

I have the following code

<input [type]="'checkbox'" [(ngModel)]="inputValue">
<p>Value: {{ inputValue }}</p>

Can somebody explain why the value in inputValue does not change?

I can't set just type="checkbox" because I have dynamic type of input.

It works fine when type is text or number. It also works when the type of input is static (type="checkbox")

4
  • i would not set the type dynamically but rather render one or the other with *ngIf Commented May 7, 2018 at 14:15
  • Is your intent to show Value: true (or false)? Commented May 7, 2018 at 14:26
  • @tt9 yes, I want inputValue to change when checkbox is checked/unchecked Commented May 7, 2018 at 14:32
  • Maybe this will help: stackoverflow.com/a/40214714/2313300 Commented May 7, 2018 at 15:01

1 Answer 1

1

If dynamically setting the input type does not work why don't you try an ngSwitch with a static input type for checkbox?

<ng-container [ngSwitch]="inputType">
    <input *ngSwitchCase="'checkbox'" type="checkbox" [(ngModel)]="inputValue">
    <input *ngSwitchDefault [type]="inputType" [(ngModel)]="inputValue">
</ng-container>

Check out this stackblitz.

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

1 Comment

Thank you.I ended up doing this. Strange that this doesn't work out of the box.

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.