How can I listen to the modal close event?

I need to know when my modal was closed. There doesn't seem to be an explicit way to do this in the same vein as ionViewDidLeave or something. I tried ionViewDidLeave and felt it doesn't work for modal close because I didn't go to this page using navController, I am showing the page using 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.  

    } 

      

+6


source to share


3 answers


You just need to listen to the modal close event in 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();
}

      



`

+11


source


Would you 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 .

+6


source


This question has been asked before Ionic 4 was released, but I think it's relevant these days. 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();
}

      

Event.ts add-on

constructor(public modalCtrl: ModalController) {}

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

      

And don't forget to include AddEventModule in HomeModule:

home.component.ts

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

      

0


source







All Articles