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.
}
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: [
...
]
})
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();
}
`
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With