[英]Angular Material: How to hide input field based on checkbox
I have a checkbox cbAddVestingOption that, when checked, should display an input field.我有一个复选框cbaddvestingoption,检查后应显示输入字段。
<mat-form-field style="width: 600px">
<mat-checkbox [formcontrolname]="cbAddVestingOption" style="padding-bottom: 200px;">Add Vesting Option</mat-checkbox>
<input type="text" placeholder="Vesting Option" matInput formcontrolname="vestedoption" [matAutocomplete]="auto" class="uppercase" />
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of vestedOptions" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
So when cbAddVestingOption is checked, vestedoption should display.因此,当检查cbaddvestingoption时,应显示既定。 When cbAddVestingOption is not c hecked, vestedoption should be hidden.
当 cbAddVestingOption 不是 c 时,应该隐藏归属选项。
Do I need to do this in the typescript or can I do it in the html?我需要在 typescript 中执行此操作,还是可以在 html 中执行此操作?
You can do it directly on the template like this:您可以像这样直接在模板上执行此操作:
<mat-checkbox #checkbox>Add Vesting Option</mat-checkbox>
<input type="text" placeholder="Vesting Option" matInput [hidden]="!checkbox.checked"/>
ADDED: A better option, using *ngIf
on the whole <mat-form-field>
:添加:更好的选择,在整个
<mat-form-field>
上使用*ngIf
:
<mat-checkbox #checkbox>Add Vesting Option</mat-checkbox>
<mat-form-field *ngIf="checkbox.checked" style="width: 600px">
<input type="text" placeholder="Vesting Option" matInput formControlName="vestedoption" [matAutocomplete]="auto" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.