且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular Reactive表单:提交后如何重置表单状态并保留值

更新时间:2022-12-07 10:31:45

@smnbbrv的解决方案效果很好.

The solution of @smnbbrv works pretty well.

您还可以将您的实际表单值提供给reset()方法.

You can also provide your actual form value to reset() method.

鉴于事实myReactiveForm是组件中的反应形式.成功提交表单后(例如通过致电服务),您可以执行以下操作:

Given the fact myReactiveForm is a Reactive form in your component. After successful submit of your form (by calling a service for example), then your can do:

this.myReactiveForm.reset(this.myReactiveForm.value);

它将重置表单并将新"表单值设置为与表单相同的值.

It will reset the form and set the "new" form values to the same value you form had.

可以在英雄之旅"示例 Angular.io官方文档

This method can be see within Tour of Hero example Official Angular.io doc