Valera
Valera

Reputation: 2923

Dynamic input of type checkbox in Angular 5

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

Upvotes: 2

Views: 651

Answers (1)

Kim Kern
Kim Kern

Reputation: 60357

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.

Upvotes: 1

Related Questions