Angular 7 reactive form how to reset the form and get its initial values instead of resetting them to empty values

alim1990 picture alim1990 · Dec 10, 2018 · Viewed 8.1k times · Source

Is there a way to reset a reactive form group into its initial values instead of get it empty using .reset() method ?

I have the following stackblitz, where the default selected value is Parent, and if the user change it to sister, and wants to get the form to initial value, click on reset button to do so.

Currently, I tried:

this.formGroup.reset();
//Or
this.formGroup.markAsPristine;
//Or
this.formGroup.markAsTouched

The .reset() reset the form completly.

P.S. Some people may say that the user can re-select the default value instead of clicking on reset button. But in my case, I have a huge user form where he needs to update his information, and he may needs to reset to initial values if he was mistaken in some values.

Answer

Ferares picture Ferares · Sep 2, 2019

You can save the form initial values:

this.initialValues = this.formGroup.value;

And then pass those values to the reset function:

this.formGroup.reset(this.initialValues);