I am using observables in my Angular app to pull in data, and then to handle navigation. But what currently happens is that the first subscription arrives, and the data adjusts accordingly. Then, after a short delay, the second subscription kicks in, handling filters and pagination, and the data changes. What I'd like to do is combine these two into one action, so that the data only loads once.
This is what I currently have:
ngOnInit()
{
this.dataService.getBySelection(this.page, this.pagesize, this.body, this.type)
.subscribe(resRecordsData => {
this.records = resRecordsData;
},
responseRecordsError => this.errorMsg = responseRecordsError
);
this.route.params.subscribe(
(params: any) => {
this.page = params['page'];
this.type = params['type'];
}
);
this.router.navigate(
['/consulting', { page: this.page || 1 , type: this.type }]);
}
UPDATE: After a commenter's suggestion, and consulting the docs, I was able to do this:
Observable.combineLatest(
this.dataService.getBySelection(this.page, this.pagesize, this.body, this.type),
this.route.params)
.subscribe(result => console.log('combineLatest: ', result));
And with that I am getting the correct data in the console. So I should be good to go.