【发布时间】:2020-07-15 18:34:36
【问题描述】:
我正在使用 angular 9 和 formController。
根据按钮点击,提供的表单添加或删除一个字段,所以我希望,当点击按钮时,表单(AND错误)全部被重置。
https://stackblitz.com/edit/angular-ivy-mku86s
我都试过了
/* Object.keys(this.form.controls).forEach((field) => {
console.log(field)
this.form.get(field).setErrors(null)
}) */
this.formDirective.resetForm()
this.form.reset()
/* this.form.updateValueAndValidity()
this.formDirective.resetForm()
this.form.reset()
this.form.markAsPristine()
this.form.markAsUntouched()
this.form.clearValidators() */
但没有任何效果...
为什么?
【问题讨论】:
-
我认为您需要为您的
ngIf语句添加一个新变量,例如showError,因为错误始终存在于 formControl 中,甚至在用户触摸表单之前也是如此。跨度> -
不应该重置全部隐藏?或
clearValidators