Tuhin Karmakar
Tuhin Karmakar

Reputation: 181

How to add an undo button to a Reactive Form (Angular 2)?

I have built a form using the Reactive Forms technique available in Angular 2. The form has many controls of different types. Now I need to add an undo button to it. I have been able to set up a basic undo button using the click and change events. I'm saving the previous value when someone clicks the form control and storing that value to Local Storage when the control's value is changed. However, this method has some drawbacks. For example, when someone tabs into the control, the click event would not fire.

So is there a way to save versions of the form when a change occurs? I think the ideal solution would be a watcher which will keep an eye on the form and save versions of it.

Upvotes: 4

Views: 3640

Answers (1)

Martino Bordin
Martino Bordin

Reputation: 1471

A bit late, but check this example here:

https://stackblitz.com/edit/reactive-form-changes-history

I used valueChanges of the form group to push the changes in an Array, so then I can restore the changes

Upvotes: 4

Related Questions