【问题标题】:how can i prevent CombineLatest to fire the first time我怎样才能防止CombineLatest第一次触发
【发布时间】:2020-03-25 16:33:19
【问题描述】:

我有一个包含一堆字段的表单(使用 reactiveForm)。 当字段A或B的值发生变化时,我想执行doStuffWithAnB。

所以 combineLatest 似乎是我需要的。

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

现在如果用户进入表单,并且只触摸 B(或 A),我想执行我的功能,> 这里是 startWith

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

但现在 doStuffWithAnB 从一开始就被触发,因为我的 2 个流有一个 startWith,但我不想在其中一个字段被修改之前执行 doStuffWithAnB

我怎样才能以干净的方式实现这一目标?

【问题讨论】:

    标签: angular rxjs angular-reactive-forms combinelatest


    【解决方案1】:

    为什么不选择merge,它会获取其中一个结果!我们也不需要startsWith,因为它最初不应该执行!

    发件人:

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

    收件人:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2015-11-21
      • 2014-06-05
      • 1970-01-01
      • 2011-06-22
      相关资源
      最近更新 更多