Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I listen for modal close event?

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.  

    } 
like image 837
Spilot Avatar asked May 30 '17 22:05

Spilot


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: [
        ...
    ]
})
like image 100
JanP Avatar answered Nov 20 '22 01:11

JanP


You Just need to listen to the modal close event in your home.ts

// listen for close event after opening the modal
    postEvent(){
        let modal = this.modalCtrl.create(AddEvent);
        modal.onDidDismiss(() => {
        // Call the method to do whatever in your home.ts
           console.log('Modal closed');
    });
    modal.present();
}

`

like image 42
Austine Iyke Avatar answered Nov 20 '22 00:11

Austine Iyke


You would just do

// home.ts
// -------

postEvent() { 

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

   modal.onDidDismiss(data => {
     // Do stuff with the data you closed the modal out with 
     // or do whatever else you need to.
   });

  modal.present();

}

You can find this in the docs.

like image 7
Hayden Braxton Avatar answered Nov 20 '22 00:11

Hayden Braxton