Angular 4 planned autosave form

I am trying to automatically save form data in Angular 4. It should work as follows:

  • The user changes some data in the form → some request to save to the database is called. Suppose that within 2 seconds a timer starts here.
  • Within 2 seconds from the previous save request, all changes will not cause any queries (to reduce the load on the database), but will cause another save request, then the 2-second timer will expire.
  • If the timer does not start at the moment, then the save request should be called immediately.

I believe that Observable , Subject and Scheduler will help me, but I'm completely new to this. Could you suggest a better approach to achieve the above functionality?

+7
javascript angular observable rxjs
source share
1 answer

You can simply subscribe to the valueChanges property on the FormGroup object associated with the auditTime statement:

 this.form.valueChanges.auditTime(2000).subscribe(formData => /* save to DB */) 

Perhaps you can also look at the throttleTime and debounceTime statements.

+4
source share

All Articles