表单代码片段
<form [formGroup]="addAttributeForm" fxLayout="column"> <mat-form-field> <input matInput formControlName="title" placeholder="Title" required> <mat-error>This field is required</mat-error> </mat-form-field> </form>
在组件中
onSubmit(form: FormGroup) { // do work form.reset(); }
我在观察的内容:
>表单值设置为空.
>但验证消息仍然显示在mat-error中.
>我尝试过form.markAsPristine(),form.markAsUntouched()并将这三者结合起来.
如何重置表单以便不显示mat-error?
要做到这一点,你需要保持FormGroupDirective
并在其上调用resetForm().
表单代码片段
<form [formGroup]="addAttributeForm" fxLayout="column"> <!-- ... --> </form>
在组件中
@ViewChild(FormGroupDirective) formDirective: FormGroupDirective; onSubmit(form: FormGroup) { // do work this.formDirective.resetForm(); }