How can I listen for modal close event?

Viewed 14181

I need to know when my modal has been closed. There doesn't seem to be an explicit way to do this in the same vein as ionViewDidLeave or something like that. I tried ionViewDidLeave and have a feeling it didn't work for the modal close because I haven't moved to that page using the navController, I'm showing the page using a modal.

home.ts

/*show modal with post form on add event page */

  postEvent(){ 

      let modal = this.modalCtrl.create(AddEvent);
      modal.present();


}

AddEvent.TS

/* Close modal and go back to feed */

     close(){

     this.viewCtrl.dismiss(); 

        //I need to change a value on the home page when this dismiss happens.  

    } 
3 Answers

This question is asked before Ionic 4 was released, but I think this is relevant these days. The Ionic 4 version:

home.ts

async postEvent() {
    const modal = await this.modalCtrl.create({
        component: AddEvent
    });
    modal.onDidDismiss().then(data => {
        console.log('dismissed', data);
    });
    return await modal.present();
}

add-event.ts

constructor(public modalCtrl: ModalController) {}

close() {
    this.modalCtrl.dismiss({
        dismissvalue: value
    });
}

And don't forget to include the AddEventModule in HomeModule:

home.component.ts

@NgModule({
    declarations: [
        ...
    ],
    imports: [
        IonicModule,
        CommonModule,
        AddEventModule
    ],
    exports: [
        ...
    ]
})
Related