【发布时间】:2021-01-17 00:10:19
【问题描述】:
我在 FormGroup 中有 5 个复选框,使用 ngFor 显示,如果选中任何一个复选框,则 1 个 matInput 必须是必填字段。
.ts
this.gridForm = this.fb.group({
cbox1: [''],
cbox2: [''],
cbox3: [''],
cbox4: [''],
cbox5: [''],
input1: ['', Validators.required] });
.html
<div *ngFor="let table of xTables; let i = index;">
<mat-checkbox formControlName="{{xTableKeys[i]}}">{{table}}</mat-checkbox>
</div>
<mat-form-field>
<input matInput formControlName="xType" placeholder="X Type">
</mat-form-field>
我已经为输入添加了必需的验证器,但我需要的是仅在选中任何复选框后才需要它。当前状态是除非我填写输入,否则我无法提交表单,但无论是否选中复选框。
【问题讨论】:
-
因为您对所有输入使用相同的 'enableInput' 变量,并且每个输入都需要唯一
标签: javascript angular typescript validation angular-forms