Angular 4计划表单自动保存 [英] Angular 4 scheduled form autosave
本文介绍了Angular 4计划表单自动保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Angular 4中实现表单数据自动保存.它应该像这样工作:
I'm trying to implement form data autosave in Angular 4. It should work like this:
- 用户以以下形式更改某些数据->调用了一些对DB的保存请求. 假设一些计时器在这里启动了2秒钟.
- 在先前保存请求的2s期间,所有更改将不会调用任何请求(以减少数据库负载),但是将触发另一个保存请求,然后2s计时器将到期.
- 如果目前没有启动计时器,则应立即调用保存请求.
- User changes some data in the form -> some save request to DB is invoked. Let's assume some timer is started here for 2s.
- During 2s from previous save request all changes will not invoke any requests (to reduce DB load), but will trigger another save request then 2s timer will expire.
- If no timer is started at the moment then save request should be invoked immediately.
我想RxJS的Observable
,Subject
和Scheduler
会对我有帮助,但是我是完全陌生的.您能否建议实现上述功能的最佳方法?
I suppose that Observable
, Subject
and Scheduler
from RxJS will help me, but I am completely new to it. Could you suggest the best approach for achieving above functionality please?
推荐答案
您可以订阅 FormGroup
对象上的c3>属性与 auditTime
运算符:
You can just subscribe to valueChanges
property on FormGroup
object chained with the auditTime
operator:
this.form.valueChanges.auditTime(2000).subscribe(formData => /* save to DB */)
也许还可以看看throttleTime
和debounceTime
运算符.
Maybe also have a look at throttleTime
and debounceTime
operators.
这篇关于Angular 4计划表单自动保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文