Set Form to Pristine without clearing data

spectacularbob picture spectacularbob · Nov 19, 2016 · Viewed 54.9k times · Source

I have a form that displays a list of <input type="text"> elements. They all share a common save button which is disabled until the form becomes dirty. Then, once the user clicks the save button, the data will be submitted to the server. If the server successfully saved the data, I want to reset the form to a pristine state, but I want to keep all of the data in the form so the user can edit the data further if they so choose.

After searching, I have found the NgForm.reset() method. While this does set the form to pristine, it unfortunately also clears out the form. The reset method does seem to have a value parameter, but I can't seem to find out what it does. Nevertheless, I don't want the data cleared out.

I have also tried myForm.pristine = true as well, but this causes a reload of the page for some reason.

Here is a plunkr demonstrating the problem.

Answer

Mohamed Fakhreddine picture Mohamed Fakhreddine · Jan 17, 2017

What you're looking for is myForm.form.markAsPristine().