Subscribe multiple FormControl(s) only once
The best option for you is combineLatest
that emits after all source Observables emitted at least one value and then on every emission from any source Observable:
Observable.combineLatest(controlA.valueChanges, controlB.valueChanges)
.subscribe(...);
Eventually you can chain each source with startWith(null)
to guarantee that every source will emit one item immediately and filter what you want in the subscriber.
Observable.combineLatest(
controlA.valueChanges.startWith(null),
controlB.valueChanges.startWith(null)
)
.subscribe(([val1, val2]) => ...);
Btw, about the other operators:
zip
- emits only when all sources emitted the same number of itemsforkJoin
- emits when all sources emitted at least one item and completed (never happens withvalueChanges
)merge
- just merges Observables but you can't know which one emitted what valuejoin
- I don't think that's part RxJS 5
For those getting combineLatest as deprecated, try combining the valueChanges observables in an array. Also, for those getting startWith as deprecated, try not using null.
combineLatest([
this.typeSelected.valueChanges.pipe(startWith('')),
this.checkedFC.valueChanges.pipe(startWith(false))
]
).subscribe(([type, checked]) => {
console.log([type, checked]);
});