Angular Form Control 导致无限循环

标签 angular angular-reactive-forms infinite-loop form-control

我有以下逻辑:

1.组件.ts

onChangeRow($event) {
    form.controls["value"].patchValue($event.data.value, { emitEvent: false }); // 
}

2.组件.ts

@Output() changeRow: EventEmitter<any> = new EventEmitter<any>();

this.formGroup.valueChanges.subscribe(data => {
  this.changeRow.emit({selfRef: this.selfRef, form: this.formGroup, config: this.config})
})

看起来第一个组件中的 patchValue 方法触发了一个循环,因为它的新值被 valueChanges 捕获,并且循环继续,直到我获得 Maximum调用堆栈大小超出 错误。有时只触发 3 次,有时只触发一次。

我尝试理解为什么emitEvent 属性在这里不起作用。

最佳答案

尝试在 patchValue() 选项参数中将 onlySelf 设置为 true

onChangeRow($event) {
    form.controls["value"].patchValue($event.data.value, { onlySelf: true, emitEvent: false }); 
}

关于Angular Form Control 导致无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67179046/

相关文章:

angular - 如何使用 Angular 从 firebase 中删除用户帐户

javascript - Angular 动态表单嵌套字段

angular - 如何在 Angular(v2 起) react 形式中找到无效控件

java - 在 "try and catch "内使用 "while loop" block 时出现无限循环

javascript - 在 Angular2 中管理不同的基本布局

rest - Angular 2如何从后端读取自定义错误消息

css - 如何使用 Angular Material 在同一行显示标签和选择框?

java - 使用扫描仪时出现无限循环?

C++ 代码崩溃并出现无限循环

angular - 如何在 Angular 5 的 Mat-Select 选项中获取所选值的 ID