angular2 routing - Proper way to handle nested subscriptions in Angular 2 -


i'm trying create pagination angular 2 web app query parameters , observables. i've gotten things work code below, have feeling nesting subscriptions isn't idea. there better way this?

  blogs:any;   page = 0;   sub;    ngoninit() {     this.sub = this.route.queryparams       .subscribe(params => {            this.page = +params['page'] || 0;            this.requestservice.getdata('posts', '..query string...')        .subscribe(data => this.blogs = data.data);      });   }   nextpage() {       this.router.navigate(['./'], { queryparams: { page: this.page + 1 }, relativeto: this.route }   );    } 

you use .switchmap chain observable.

ngoninit() {   this.sub = this.route.queryparams   .switchmap(params => {        this.page = +params['page'] || 0;        return this.requestservice.getdata('posts', '..query string...')   })   .subscribe(data => this.blogs = data.data); } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -