我正在使用Reactive Forms创建一个包含三种不同表单的页面。我想听听他们的所有更改,如果它们都是有效的,请做一些逻辑。我在论坛上检查了一下,发现必须使用forkJoin。问题在于,每次加载页面时都会调用这些更改,但在我的值更改时不会触发。
forkJoin([
of(this.thirdStepFormFirstContainer.valueChanges),
of(this.thirdStepFormSecondContainer.valueChanges),
of(this.thirdStepFormThirdContainer.valueChanges)
]).subscribe(() => {
console.log('One of the three has changed');
});
valueChanges返回一个Observable,仅在更改时才应在页面加载时启动forkJoin。
有人知道我该怎么解决吗?
非常感谢:)
答案 0 :(得分:3)
如果您希望在三个表单控件的值之一发生更改时触发订阅,则可以使用merge
函数。
merge(
this.thirdStepFormFirstContainer.valueChanges,
this.thirdStepFormSecondContainer.valueChanges,
this.thirdStepFormThirdContainer.valueChanges
).subscribe(() => {
console.log('One of the three has changed');
});
merge
将侦听所有三个表单控件的更改,并且当任何一个控件发出值更改时,您的订阅回调将得到通知。