如何在反应形式(角度2)中添加撤消按钮? [英] How to add an undo button to a Reactive Form (Angular 2)?

查看:71
本文介绍了如何在反应形式(角度2)中添加撤消按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Angular 2中可用的Reactive Forms技术构建了一个表单.该表单具有许多不同类型的控件.现在,我需要向其添加一个撤消按钮.我已经能够使用clickchange事件设置一个基本的撤消按钮.当有人单击表单控件时,我将保存先前的值,并在控件的值更改时将该值存储到本地存储"中.但是,这种方法有一些缺点.例如,当某人 tab 进入控件时,click事件将不会触发.

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.

那么,有没有一种方法可以在发生更改时保存表单的版本?我认为理想的解决方案是使用 watcher ,它可以关注表单并保存其版本.

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.

推荐答案

有点晚了,但是请在此处查看以下示例:

A bit late, but check this example here:

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

我使用了表单组的valueChanges将更改推送到Array中,因此可以恢复更改

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

这篇关于如何在反应形式(角度2)中添加撤消按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆