简体   繁体   English

我怎样才能防止 CombineLatest 第一次触发

[英]how can i prevent CombineLatest to fire the first time

I have a form with a bunch of fields (using reactiveForm).我有一个包含一堆字段的表单(使用reactiveForm)。 When the value change in field A or B, I want to execute doStuffWithAnB.当字段 A 或 B 中的值发生变化时,我想执行 doStuffWithAnB。

so combineLatest seems to be what i need.所以 combineLatest 似乎是我需要的。

combineLatest(A.valueChanges,B.valueChanges).subscribe(doStuffWithAnB)

now if the user goes in the form, and touches only B (Or A), I want to execute my function, > here comes startWith现在,如果用户进入表单,并且只触摸 B(或 A),我想执行我的函数,> 这里是 startWith

combineLatest(A.valueChanges.pipe(startWith(someDefaultValue),B.valueChanges.pipe(startWith(someOtherDefaultValue)).subscribe(doStuffWithAnB)

but now doStuffWithAnB is fired from the start since my 2 streams have a startWith, but i don't want to execute doStuffWithAnB until one of the fields has been modified但现在 doStuffWithAnB 从一开始就被触发,因为我的 2 个流有一个 startWith,但我不想执行 doStuffWithAnB,直到其中一个字段被修改

how can i achieve that in a clean way ?我怎样才能以一种干净的方式实现这一目标?

Why not go for a merge instead which will take either one of the results!为什么不进行merge而是采用其中一个结果! We also do not need startsWith , since it should not execute initially!我们也不需要startsWith ,因为它最初不应该执行!

From:从:

 combineLatest(A.valueChanges.pipe(startWith(someDefaultValue),B.valueChanges.pipe(startWith(someOtherDefaultValue)).subscribe(doStuffWithAnB)

To:到:

merge(A.valueChanges,B.valueChanges).subscribe(doStuffWithAnB)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM