如何清除角度控件中的错误

Jas*_*ick 5 angular

如何在 angular 中设置自定义错误非常清楚:

this.form.controls['field'].setErrors({same:true});
Run Code Online (Sandbox Code Playgroud)

但目前尚不清楚如何删除它。有人知道怎么做吗?任何替代方案?

Max*_*Max 6

只需将错误设置为空

this.form.controls['field'].setErrors(null);


Jul*_*ova 0

this.form.controls['field'].updateValueAndValidity();
Run Code Online (Sandbox Code Playgroud)

这是方法文档:

/**
  * Re-calculates the value and validation status of the control.
  *
  * By default, it will also update the value and validity of its 
 ancestors.
  */
 updateValueAndValidity({onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}):  void {... }
Run Code Online (Sandbox Code Playgroud)