【发布时间】:2018-06-21 07:54:51
【问题描述】:
我的页面上有一个表单,当我调用FormGroup.reset() 时,它会将表单类设置为ng-pristine ng-untouched,但FormControl.hasError(...) 仍然返回truthy。我在这里做错了什么?
模板
<form [formGroup]="myForm" (ngSubmit)="submitForm(myForm)">
<mat-form-field>
<input matInput formControlName="email" />
<mat-error *ngIf="email.hasError('required')">
Email is a required feild
</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput type="password" formControlName="password" />
<mat-error *ngIf="password.hasError('required')">
Password is a required feild
</mat-error>
</mat-form-field>
<button type="submit">Login</button>
</form>
组件
export class MyComponent {
private myForm: FormGroup;
private email: FormControl = new FormContorl('', Validators.required);
private password: FormControl = new FormControl('', Validators.required);
constructor(
private formBuilder: FormBuilder
) {
this.myForm = formBuilder.group({
email: this.email,
password: this.password
});
}
private submitForm(formData: any): void {
this.myForm.reset();
}
}
Plunker
【问题讨论】:
-
你能不能也打电话给
this.myForm.markAsUntouched();? -
这不起作用,根据文档不应该是必需的。 (v2.angular.io/docs/ts/latest/api/forms/index/…)
标签: html angular typescript angular-material angular-reactive-forms