首页 > 解决方案 > Angular 5中类型复选框的动态输入

问题描述

我有以下代码

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

有人可以解释为什么 in 的值inputValue不会改变吗?

我不能仅仅type="checkbox"因为我有动态类型的输入而设置。

当 type 为textor时它工作正常number。它也适用于输入类型为静态 ( type="checkbox")

标签: javascriptangularangular-forms

解决方案


如果动态设置输入类型不起作用,为什么不尝试ngSwitch使用静态输入类型作为复选框?

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

看看这个stackblitz


推荐阅读