如何在angular2中选中复选框时动态生成textarea

时间:2022-06-15 19:42:00

I am new to Angular2, trying to generate textarea dynamically when checkbox is checked and delete textarea when checkbox is unchecked in angular2., Is there any better solution? thanks in advance.

我是Angular2的新手,在选中复选框时尝试动态生成textarea,并在angular2中取消选中复选框时删除textarea。是否有更好的解决方案?提前致谢。

2 个解决方案

#1


0  

Set ngModel and then store it as a variable:

设置ngModel,然后将其存储为变量:

<input type="checkbox" #isVisible="ngModel" [(ngModel)]="isTextareaVisible">

Then use an *ngIf to toggle it:

然后使用* ngIf切换它:

<textarea *ngIf="isVisible"></textarea>

Sidenote: If this is inside a form tag, don't forget to add a name="isTextareaVisible" attribute to the input, or angular will complain.

旁注:如果这是在表单标记内,请不要忘记在输入中添加name =“isTextareaVisible”属性,否则angular会抱怨。

#2


0  

<input type="checkbox" #mycheckbox />
<textarea *ngIf="mycheckbox.value"></textarea>

#1


0  

Set ngModel and then store it as a variable:

设置ngModel,然后将其存储为变量:

<input type="checkbox" #isVisible="ngModel" [(ngModel)]="isTextareaVisible">

Then use an *ngIf to toggle it:

然后使用* ngIf切换它:

<textarea *ngIf="isVisible"></textarea>

Sidenote: If this is inside a form tag, don't forget to add a name="isTextareaVisible" attribute to the input, or angular will complain.

旁注:如果这是在表单标记内,请不要忘记在输入中添加name =“isTextareaVisible”属性,否则angular会抱怨。

#2


0  

<input type="checkbox" #mycheckbox />
<textarea *ngIf="mycheckbox.value"></textarea>