angular - 我怎样才能防止CombineLatest第一次触发
问题描述
我有一个包含一堆字段的表单(使用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,直到其中一个字段被修改
我怎样才能以干净的方式实现这一目标?
解决方案
为什么不去merge
取而代之,它将采取任何一个结果!我们也不需要startsWith
,因为它最初不应该执行!
从:
combineLatest(A.valueChanges.pipe(startWith(someDefaultValue),B.valueChanges.pipe(startWith(someOtherDefaultValue)).subscribe(doStuffWithAnB)
至:
merge(A.valueChanges,B.valueChanges).subscribe(doStuffWithAnB)
推荐阅读
- javascript - CSS Style First Row and Column
- sql-server - 当我在函数的表达式部分计算两列的总和时,ISNULL 不起作用
- php - 将 ACF 值发送到电子邮件表单 - Wordpress
- json - Flutter - 访问 JSON 嵌套数组值
- r - 使用虚拟数据框设置文本数据的绘图区域
- definition - EEMEM 代表什么?
- javascript - 它需要计算值,但它不起作用 javascript
- nginx - 用于 Kubernetes 的 Nginx 解析器与 skydns
- swift - Swift 4.1 中的 SpriteNode 移动和旋转
- r - 许多位置的反向地理编码搜索(国家名称),当国家丢失时输出到数据框问题