我计划在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
发布评论