Angular Reactive forms : how to reset form state and keep values after submit

前端 未结 7 1864
逝去的感伤
逝去的感伤 2020-12-29 18:42

In an Angular reactive form. How to reset only the state of form after successful submit?

Here is the process:

  • Create the form and
7条回答
  •  醉酒成梦
    2020-12-29 19:14

    For reactive forms what worked for me when using angular 7: was to use template-driven form and pass it via the form submit handler like so

    // component.html
     
    ....
    // component.ts onSubmit(form: NgForm) { // reset form here form.form.markAsPristine(); form.resetForm(); }

    It will reset the form and the submitted state to default.

提交回复
热议问题