typescript patchValue 和 { emitEvent: false } 触发 Angular 4 表单组上的 valueChanges

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45241103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:44:46  来源:igfitidea点击:

patchValue with { emitEvent: false } triggers valueChanges on Angular 4 formgroup

angulartypescriptionic-frameworkangular2-formbuilderangular-reactive-forms

提问by larpo

I have a formbuilder group and am listening for changes with valueChanges and triggering a save function followed by refresh function on the form:

我有一个 formbuilder 组,正在监听 valueChanges 的更改并触发保存功能,然后是表单上的刷新功能:

 this.ticketForm.valueChanges.debounceTime(1000).distinctUntilChanged()
 .subscribe(data => {
   this.saveTicket();
   this.refreshTicket();
 })

I am then reloading the form and repatching the data to form fields (and elsewhere on the page, particularly a change log) with patchValue, e.g.:

然后我重新加载表单并使用 patchValue 将数据重新分配到表单字段(以及页面上的其他地方,尤其是更改日志),例如:

    this.ticketForm.patchValue(ticket, { emitEvent: false });

however, this causes an infinite loop of saves of the form despite emitEvent : false.

然而,尽管有emitEvent : false,这会导致表单保存的无限循环。

Is this an Angular 4/Ionic 3 bug or a misunderstanding on my part?

这是 Angular 4/Ionic 3 的错误还是我的误解?

回答by Lakshay

Try adding onlySelf: truealong with the emitEvent: falsein this way:

试着增加onlySelf: true与一起emitEvent: false以这种方式:

this.ticketForm.patchValue(ticket, {emitEvent: false, onlySelf: true});

回答by Wei Lun

As a workaround, i add skip to rxjs pipe

作为一种解决方法,我将跳过添加到 rxjs 管道

this.form.valueChanges
    .pipe(skip(1)).subscribe();