gpt4 book ai didi

angular - combineLatest 与 FormControl valueChanges 事件绑定(bind)不发出

转载 作者:太空狗 更新时间:2023-10-29 17:30:54 26 4
gpt4 key购买 nike

我通过 2 [formControl] "toppings":array 和 "toppings2":array 收听 2 种形式。

我必须同时拥有表单的 2 个值。所以我将我的两个可观察值与“CombineLatest”结合起来

我的代码:

ngOnInit() {
combineLatest(this.toppings.valueChanges, this.toppings2.valueChanges)
.subscribe(val => console.log(val))
}

但是现在,在组件的初始化中,只有一种形式发送console.log(val)。

如果我在收到第二个表单的日志后单击此表单。你遇到过这个问题吗?

最佳答案

您可能希望两个 Observable 都有一个初始值。 combineLatest 仅在所有 Observable 都已发出至少一个值时才会发出。使用 startWith 运算符创建此行为,如下所示:

combineLatest(
this.toppings.valueChanges.pipe(startWith("")),
this.toppings2.valueChanges.pipe(startWith("")))

或者,如果您有可用的初始值,就像建议的那样:

combineLatest(
this.toppings.valueChanges.pipe(startWith(this.toppings.value)),
this.toppings2.valueChanges.pipe(startWith(this.toppings2.value)))

注意,这将使用初始值发出一次。要抑制此行为,您可以使用 skip(1) 运算符忽略此初始通知。

关于angular - combineLatest 与 FormControl valueChanges 事件绑定(bind)不发出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52181631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com