角度反应式表单组清除组件中的错误未从模板中清除



>我正在动态清除我的反应式形式的错误,例如:

this.courseForm.setErrors(null);

但是错误仍然显示在我的模板中:

<form [formGroup]="courseForm" autocomplete="off">
<div class="alert-group" *ngIf="submitted && formGroup.errors?.coursedates">
<div *ngFor="let err of formGroup.errors?.coursedates?.value" role="alert" class="error">
{{err}}
</div>
</div>
</form>

有没有办法清除模板中的表单组错误?

谢谢

每个控件的错误不会填充表单组错误。因此,清除表单组错误不会从其每个表单控件中删除错误。

我们需要为表单组中的每个控件设置错误(null(。以下是我们的做法。

Object.keys(this.courseForm.controls).forEach(key => {
this.form.get(key).setErrors(null);
});

如果解决方案解决了您的问题,请确保将其标记为答案。

最新更新