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

蓝咒 提交于 2020-01-04 04:07:10

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/44355194/how-to-add-an-undo-button-to-a-reactive-form-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!