【发布时间】:2018-12-24 09:23:57
【问题描述】:
使用动态表单时,角度材质 mat-stepper 出现问题。例如,我有一个步进器,它有 3 个步骤,每个步骤都有自己的形式。然而,第一步使用隐藏表单来确定它是否有效,因为此步骤可以动态添加更多表单,因此无法将所有表单绑定到该步骤。
当您迈出第一步时,您可以创建多个表单,并且一切都按预期工作,而不会对添加的新表单进行任何随机验证。如果您转到第 2 步或第 3 步并返回到第一步,然后创建一个新表单,它将自动以红色突出显示的所有字段开始。
我已经尝试了许多不同的尝试来压制,但我都没有成功。下面是一个基本示例,说明我的第一步如何包含绑定到步骤控件的 hiddenForm、默认表单和在该步骤中创建更多表单的按钮。
我对尝试解决此问题的研究使我相信材料步进器会使所有 mat-form-fields 在无效时变为红色,无论是否新添加。
<mat-horizontal-stepper [linear]="true">
<mat-step [stepControl]="hiddenForm" label="step 1"
<form [formGroup]="myForm">
<mat-form-field>
<mat-label>
First Name
</mat-label>
<input [formControl]="firstName"matInput>
<mat-error *ngIf="!firstName.valid && firstName.touched">
First Name required
</mat-error>
</mat-form-field>
</form>
<button (click)="AddNewForm()">Add New Form</button>
</mat-step>
</mat-horizontal-stepper>
尝试失败:(当前表单是新添加的表单)
this.currentForm.markAsUntouched();
this.currentForm.markAsPristine();
this.currentForm.setErrors(null);
this.currentForm.reset();
this.currentForm.get('firstName).setErrors(null);
this.currentForm.get('firstName).reset();
this.currentForm.get('firstName).markAsPristine();
this.currentForm.get('firstName).markAsUntouched();
<mat-step [completed]="true" ..> ( on all steps )
【问题讨论】:
标签: angular validation dynamic angular-material dynamic-forms