相关疑难解决方法(0)

Angular 5 FormGroup重置不会重置验证器

我在我的页面上有一个表单,当我调用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>
Run Code Online (Sandbox Code Playgroud)

零件

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
    }); …
Run Code Online (Sandbox Code Playgroud)

angular angular-reactive-forms

47
推荐指数
5
解决办法
4万
查看次数

在reset()表单之后出现角度验证消息

我尝试在调用之后使表格变得清晰而没有验证错误消息reset()。

我的表单在加载时看起来很干净,这是可以预期的: 加载页面时的形式,没有验证错误 但是,如果用户按下注册按钮,并且出现错误,则触发该form.reset()方法。我希望表格看起来像上面的图片,因为触摸,原始,肮脏的道具都与表格初次加载时一样。而是清除了值,但显示了验证错误。 带有验证错误的表格 谁能帮我将其恢复为初始状态,是否显示无验证错误的清晰表格?这是一种反应形式。如果您需要更多信息,请与我们联系。谢谢!

forms angular-validation angular angular-reactive-forms

5
推荐指数
1
解决办法
1173
查看次数