简体   繁体   English

使用 Angular Reactive Forms 时实现自动保存到 Firestore 数据库

[英]Implement Autosave to Firestore database when using Angular Reactive Forms

I have an Angular app where the data in a form needs to be saved to (and updated from) a Firestore database more or less instantly since 1) multiple people will be entering data on the same form, 2) other users are going to be using the real time values in another part of the application, and 3) the data needs to be saved in case of power or internet failure.我有一个 Angular 应用程序,其中表单中的数据需要或多或少地立即保存到(并从)Firestore 数据库更新,因为 1)多人将在同一个表单上输入数据,2)其他用户将在应用程序的另一部分使用实时值,以及 3) 数据需要在电源或互联网故障的情况下保存。

I'm using reactive forms and I've tried saving the form values by subscribing to the form changes and also by triggering individual changes on keyup.我正在使用响应式表单,并且我尝试通过订阅表单更改以及触发 keyup 上的单个更改来保存表单值。 For the most part, it works ok, but sometimes the data gets lost.大多数情况下,它工作正常,但有时数据会丢失。

For example, let's say I type something into a field (eg 123456789).例如,假设我在字段中输入了一些内容(例如 123456789)。 Here is what sometimes happens:以下是有时会发生的情况:

  • As I'm typing, '1234' gets saved to the database当我输入时,'1234' 被保存到数据库中
  • I continue typing '567'我继续输入“567”
  • Once 1234 is updated in the database, it gets emitted to the document's valueChanges observable一旦 1234 在数据库中更新,它就会被发送到文档的 valueChanges observable
  • The form gets patched with '1234'表单被“1234”打上补丁
  • I continue typing '89'我继续输入“89”
  • The value in the input is now '123489' (discarding the 567 since that got lost when the update happened)输入中的值现在是“123489”(丢弃 567,因为更新发生时丢失了)

Is there a good way to handle this?有没有好的方法来处理这个问题? I've tried to use debounceTime, but that sort of exacerbates the delay problem.我曾尝试使用 debounceTime,但这会加剧延迟问题。

I've got a sample stackblitz where I simulate the firestore database delay issue ( https://stackblitz.com/edit/immediate-save?file=src/app/app.component.ts ).我有一个示例 stackblitz,我在其中模拟了 Firestore 数据库延迟问题( https://stackblitz.com/edit/immediate-save?file=src/app/app.component.ts )。

ps I'd be OK with holding off on a save until the focus changes (eg blur or (change)), but I would also want to trigger a save if a cursor stops typing in a field, but doesn't tab to the next field ps 我可以在焦点改变(例如模糊或(改变))之前推迟保存,但如果光标停止在字段中输入,但我也想触发保存,但没有标签到下一个字段

How does adding debounce time exacerbates delay problem?添加去抖时间如何加剧延迟问题? Just add debounce on valueChanges lisener, not database request:只需在 valueChanges lisener 上添加 debounce,而不是数据库请求:

  this.controls.valueChanges
    .pipe(
      debounceTime(500)
    )
    .subscribe(person=>{
      console.log(person);
      this.dataSvc.updatePerson(person);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular Firestore唯一名称验证(反应形式) - Angular Firestore Unique name validation (Reactive Forms) 在 Angular 中使用响应式表单与服务 - Using reactive forms in Angular with services Angular 中的 'missingFormException' 使用 Reactive forms? - 'missingFormException ' in Angular using Reactive forms? Angular 9 Reactive Forms:使用 trackBy 时复选框未更新 - Angular 9 Reactive Forms: checkbox not getting updated when using trackBy 使用minlength的角反应形式仅在长度为0时显示错误 - Angular reactive forms using minlength only shows error when length is 0 在Angular 5中使用反应性表单时,请在验证之前标记所需的formControls - Mark required formControls before Validation when using Reactive Forms in Angular 5 在 Angular 中使用类型化的 Reactive Forms 时如何确定 FormControl 的类型? - How to determine the type of a FormControl when using typed Reactive Forms in Angular? 使用我自己的验证器时出错(Angular、Reactive-Forms) - Error when using my own validator (Angular, Reactive-Forms) Angular 反应性 Forms AddControl 在使用 PatchValue 时导致 ExpressionChangedAfterItHasBeenCheckedError - Angular Reactive Forms AddControl Causes ExpressionChangedAfterItHasBeenCheckedError When Using PatchValue 何时在 Angular 中使用模板驱动和反应式 forms - When to use template driven & reactive forms in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM