订阅 Angular 6
2018-09-04
71
toppings : Array
toppings2 : Array
我有 2 个订阅方法:
this.toppings.valueChanges.subscribe(val=> {
console.log(val);
}
this.toppings2.valueChanges.subscribe(val2=> {
console.log(val2);
}
我想在同一个函数中使用 val 和 val2。如何在同一个地方获取 val 和 val2?(或 val3= val+val2)
2个回答
combineLatest 将为您提供 toppings 和 toppings2 的最新版本。
https://www.learnrxjs.io/operators/combination/combinelatest.html
大理石图: http://rxmarbles.com/#combineLatest
示例:
import {combineLatest} from 'rxjs';
combineLatest(this.toppings.valueChanges, this.toppings2.valueChanges )
.subscribe( ([topping1val, topping2val]) => {
let topping3 = topping1val + topping2val;
});
user20501
2018-09-04
zip
组合两个可观察对象并等待它们都发出。combineLatest 也可以,你不需要它,
zip(this.toppings.valueChanges, this.toppings2.valueChanges).subscribe(val => console.log(val[0] + val[1]))
Sajeetharan
2018-09-04