如何实现表单的自动保存?(How to implement auto save for a form?)

编程入门 行业动态 更新时间:2024-10-27 20:25:44
如何实现表单的自动保存?(How to implement auto save for a form?)

我计划在FormBuilder的帮助下使用ReactiveForms。 我想知道自动保存可以实施的方式。 我需要以下行为:

页面上没有提交按钮。 当用户在表单中进行更改时,数据会自动保存并发送到服务器。

如何在Angular2中实现这一目标?

谢谢。

I plan on using ReactiveForms with the help of FormBuilder. And I'm wondering in which way auto save can be implemented. I need the following behaviour:

There's no submit button on a page. When user makes changes within a form, the data is automatically saved and gets sent to a server.

How can this be achieved in Angular2?

Thank you.

最满意答案

您可以订阅表单的valueChanges事件。 然后只需检查表单是否有效,然后在每次更改时保存数据。

this.form = new FormGroup({...});
this.form.valueChanges.subscribe(val => { this.saveMyFormValue(val); });
 

valueChanges是抽象类AbstractControl一个属性。 FormGroup , FormControl和FormArray都实现了AbstractControl ,这意味着如果您愿意,也可以为单个表单控件订阅此事件。

You can subscribe to the valueChanges event of your form. Then simply check if the form is valid and then save your data on every change.

this.form = new FormGroup({...});
this.form.valueChanges.subscribe(val => { this.saveMyFormValue(val); });
 

valueChanges is a property on the abstract class AbstractControl. FormGroup, FormControl and FormArray all implement AbstractControl, meaning that you can also subscribe to this event for individual form controls if you'd wish to.

更多推荐

ReactiveForms,自动,保存,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-07-15 15:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1115346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   如何实现   自动保存   implement   save

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!