1

I'm trying to present a modal during route change for a form, but getting stuck with how to return the result to ionViewCanLeave.

ionViewCanLeave(): boolean {
    debugger;
    let modal = this.modalCtrl.create(this.cancelModal);
    let returnStatement = null;
    modal.onDidDismiss(data => {
      debugger;
      returnStatement = data;
    });
    modal.present();
    if(returnStatement !== null ){
      return returnStatement;
    }
  }

How would I wait for onDidDismiss to trigger before calling the return statement for ionViewCanLeave?

1 Answer 1

1

I think you'd better change your pattern to do what you want.

As fas as I know there is no way to stop ionViewCanLeave() returning.

Thus, make another function which asynchronously executes leave page function after modal onDidDismiss like the below.

checkCanLeave(){
  debugger;
  let modal = this.modalCtrl.create(this.cancelModal);
  modal.onDidDismiss(data => {
    debugger;
    // Put on your leave page function like 'this.navCtrl.pop()' orr 'this.viewCtrl.dismiss'
  });
  modal.present();
}
2
  • 1
    I ended up using async/await on ionViewCanLeave() as suchasync ionViewCanLeave(){ if(this.submitted){ return true; } else { const shouldLeave = await this.confirmLeave(); return shouldLeave; } } Commented Jul 19, 2018 at 23:39
  • Yes, that seems correct way. I have just glanced at view-controller.ts in github. And I find it out ionViewCanLeave to be able to return boolean | string | Promise<any>.
    – Hyuck Kang
    Commented Jul 20, 2018 at 1:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.