0

sorry for the confusing question. So currently i'm working with this block of code in Ionic, it looks like this:

home.ts :

ionViewDidLoad() {
    this.myFunction().subscribe()( data => {

        // Do some stuff here, then 
        // open another page/modal with the data received

        this.openModal(anotherPage,data);
    }
}

The problem, I believe, is that I have to do something to "unsubscribe" my current function since it's being called every second. I tried putting the whole code in the ionViewDidLoad, believing that when it jumps to another page the function will be suspended but it just doesn;t work.

openModal(zone) {
    let modal = this.modalCtrl.create(ZonePage, { 'zone': zone });
    modal.present();
  }

Any idea to solve this situation?

ken.ng
  • 221
  • 4
  • 15

1 Answers1

1

one solution could be:

ionViewDidLoad() {
    let sub = this.myFunction().subscribe( data => {
        // Do some stuff here, then 
        // open another page/modal with the data received
        sub.unsubscribe();
        this.openModal(anotherPage,data);
    }
}

another one (when you know how often it emits before it you trigger your modal):

ionViewDidLoad() {
    this.myFunction().take(10).subscribe( data => {
        // Do some stuff here, then 
        // open another page/modal with the data received
        this.openModal(anotherPage,data);
    }
}
fastr.de
  • 1,497
  • 14
  • 23