Reputation: 137
I need to subscribe to two different things in the ngOnInit of a given component:
The issue here is that I need somehow to concat them in a given order (1. then 2.) because the function called in 2 setSelected(params["sourceTool"] is using the list of items mentioned.
Could it be done with rxjs concat operator? How should it be coded?
Thanks
ngOnInit(): void {
this.searchApi.getSourceTools()
.subscribe(response => {
response.items.forEach((sourceTool: string) => {
this.sourceOptions.push({
description: sourceTool,
selected: false
})
})
})
this.route.queryParams
.subscribe(params => {
this.setSelected(params["sourceTool"])
})
}
Upvotes: 1
Views: 459
Reputation: 78
this.searchApi.getSourceTools()
.pipe(
tap(response => {
response.items.forEach((sourceTool: string) => {
this.sourceOptions.push({
description: sourceTool,
selected: false
})
})
}),
switchMap(() => this.route.queryParams)
)
.subscribe(params => {
this.setSelected(params["sourceTool"])
});
If I understand correctly your problem, you can easily use switchMap
operator to change the source observable after initial source emit value (with side effects execution)
Upvotes: 1