我需要知道我的模态何时关闭。似乎没有一种明确的方法可以与IonviewDidleave或类似的东西相同的静脉来做。我尝试了IonviewDidleave,并有一种感觉,它对模态关闭不起作用,因为我没有使用NavController移动到该页面,我正在使用模态显示页面。
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.
}
您只需要收听家庭中的模态关闭事件。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();
}
`
在释放离子4之前,问这个问题,但我认为这几天很重要。离子4版本:
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
});
}
,不要忘记将AddEventModule包括在Homememodule中:
home.component.ts
@NgModule({
declarations: [
...
],
imports: [
IonicModule,
CommonModule,
AddEventModule
],
exports: [
...
]
})
您只需
// 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();
}
您可以在文档中找到它。