从验证中重置Angular 7反应性 [英] Reset Angular 7 Reactive From Validation

查看:67
本文介绍了从验证中重置Angular 7反应性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular Reactive表单作为搜索表单.我希望能够重置表格.我使用以下代码进行了此操作:

I am using an Angular Reactive form as a search form. I want to be able to reset the form. I did this with the following code:

<button type="reset" (click)="onReset()">
    Reset
</button>

重置方法执行以下操作:

The reset method does the following:

onReset(){
    this.myForm.reset();
    this.myForm.markAsPristine();
    this.myForm.markAsUntouched();
}

这将使所有表单控件为空.但是它不会重置表单验证.如果表单无效,我将停用提交按钮.当我第一次使用该表格时,此方法有效.单击重置后,验证不再起作用.提交表格后,它似乎显得ak废了.

This makes all the form controls empty. But it does not reset the form validation. I deactivate the submit button if the form is not valid. This works when I first use the form. After I click on reset the validation does not work anymore. It seems to be deaktivated after submitting the form.

我该如何解决?我在这里想念什么?

How can I solve this? What am I missing here?

推荐答案

您可以使用 clearValidators()删除特定formGroup/formcontrol上的验证用于反应形式.

You can remove validations on specific formGroup/formcontrol by using clearValidators() for reactive forms.

 this.formGroup.clearValidators() or      
 this.formGroup.controls.controlName.clearValidators()

此后,您必须使用删除的验证器更新表单控件

After this, you have to update form control with the removed validator

this.formGroup.controls.controlName.updateValueAndValidity()

这有助于我解决相同的问题,希望对您有帮助

This helped me to resolve same issue, hope it helps you to

这篇关于从验证中重置Angular 7反应性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆