Avocado
Avocado

Reputation: 327

How to reset Angular reactive form without clearing pre populated data?

I have a angular reactive form and some of the fields in the form are pre populated. I want to implement a 'Cancel' button that will reset all the changes to the form except the data that were pre populated when page was loaded. I tried the following but it resets the whole form:

resetForm() {
    this.myForm.reset();
  }

is there a way to reset only the current changes instead of resetting the whole form?

Upvotes: 1

Views: 780

Answers (1)

N.F.
N.F.

Reputation: 4182

You can do it with patchValue

resetForm() {
  this.myForm.patchValue(initialValue);
}

initialValue is a object that holds populated values.

Upvotes: 3

Related Questions