HTML:
<form [formGroup]="locationForm" #myForm="ngForm" class="formContainer">
<div class="buttonContainer">
<section class="btn1">
<button
(click)="addAnotherMethod()"
mat-raised-button
color="primary"> Add another method </button>
</section>
<section>
<button
(click)="addAnotherLocation()"
mat-raised-button
color="primary"> Add another location </button>
</section>
</div>
.TS 文件
@ViewChild('myForm') currentLocationForm;
addAnotherLocation(): void{
if(this.locationForm.valid){
//send data to redux
}))
}
this.currentLocationForm.resetForm()
}
addAnotherMethod: void {
if(this.locationForm.valid){
//send data to redux
}))
}
this.currentLocationForm.resetForm()
}
使用上面的代码,我可以在将数据发送到 redux 后重置表单,但重置后,新表单中的验证错误消息出现说要填写必填字段。我需要帮助以使用正确的方法重置此表单并在显示新表单时清除验证。我也可能使用错误的方法来调用两个按钮功能。谢谢你。
您的表单中有两件事:FormGroup
和FormGroupDirective
. 要消除错误,您需要重置它们:
this.currentLocationForm.resetForm();
this.locationForm.reset();
有关原因的更多详细信息,请参阅此帖子。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句