angular6:如何在隐藏和显示表单控件之间切换

时间:2018-12-16 05:32:14

标签: forms angular5 angular6 angular-reactive-forms form-control

我无法使用嵌入Angular6反应形式的选择在隐藏和显示控件之间切换。我想要当用户在garde表单控件中选择option3时,显示表单controll的名称。

this.form = this.fb.group({
  id: [null, Validators.compose([Validators.required])],
  name: [null, Validators.compose([Validators.required])],
  grade: [null, Validators.compose([Validators.required])],

});

.html

<mat-form-field class="mb-1">
<input  matInput  " [formControl]="form.controls['id']">
</mat-form-field>


<mat-form-field class="mb-1">
<mat-select  [formControl]="form.controls['grade']">

<mat-option value="option1">option1</mat-option>
<mat-option value="option2">option2</mat-option>
<mat-option value="option3">option3</mat-option>
</mat-select>
</mat-form-field>

<mat-form-field class="mb-1">
<input  matInput  " [formControl]="form.controls['name']">
</mat-form-field>

1 个答案:

答案 0 :(得分:0)

最快的解决方案是,尽管不是很优雅:

<mat-form-field class="mb-1" *ngIf="form.controls['grade'].value == 'option3'">
  <input  matInput [formControl]="form.controls['name']">
</mat-form-field>