简体   繁体   English

markAsUntouched 不清除红线

[英]markAsUntouched not clear the red line

I'm using reactive form with Angular Material.我正在使用带有 Angular Material 的反应形式。 When I click onsubmit() I want that all params that don't have a value will not show with error(red line)当我单击 onsubmit() 时,我希望所有没有值的参数都不会显示错误(红线)

I try to use this.formGroup.markAsUntouched();我尝试使用this.formGroup.markAsUntouched(); but it didn't work.但它没有用。

When I don't use on Angular Material, it works.当我不在 Angular Material 上使用时,它会起作用。 Does someone know how to do it right with Angular Material?有人知道如何正确使用 Angular Material 吗?

<form [formGroup]="formGroup" (ngSubmit)="onSubmit(formGroup.value)" class="form">
    <mat-form-field class="form-element">
        <input matInput placeholder="Emailaddress" formControlName="email">
    </mat-form-field>
</form>



onSubmit(post) {
    this.formGroup.markAsUntouched();
}

The formGroup will be untouched but the red lines still appears. formGroup 将保持不变,但仍会出现红线。

formReset(formGroup: FormGroup) {
    formGroup.control.reset();
    formGroup.control.markAsPristine();
    formGroup.control.markAsUntouched();
}

// for safety measure try calling this function in a try-catch block, it works on a reactive forms

如果出现红色下划线,则可以重置所有 FormGroup 错误this.formGroup.setErrors(null);

Use this approach to suppress all error message使用此方法抑制所有错误消息

Object.keys(this.mygroup.controls).map((field) => {
      const control = this.mygroup.get(field);
      if (control instanceof FormControl) {
        control.setErrors(null);
      }  
    });

Or to individually suppress any message或单独抑制任何消息

this.mygroup.controls.frmCtrlMyControl.setErrors(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM