在不清除数据的情况下将 Form 设置为 Pristine [英] Set Form to Pristine without clearing data

查看:21
本文介绍了在不清除数据的情况下将 Form 设置为 Pristine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示 元素列表的表单.它们都共享一个通用的保存按钮,该按钮在表单变脏之前一直处于禁用状态.然后,一旦用户单击保存按钮,数据就会提交到服务器.如果服务器成功保存了数据,我想将表单重置为原始状态,但我想保留表单中的所有数据,以便用户可以根据需要进一步编辑数据.

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.

经过搜索,我找到了NgForm.reset()方法.虽然这确实将表单设置为原始状态,但不幸的是它也会清除表单.reset 方法似乎有一个 value 参数,但我似乎无法找出它的作用.尽管如此,我不想清除数据.

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.

我也尝试过 myForm.pristine = true ,但由于某种原因这会导致页面重新加载.

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

这是一个演示问题的 plunkr.

推荐答案

您正在寻找的是 myForm.form.markAsPristine().

这篇关于在不清除数据的情况下将 Form 设置为 Pristine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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