我怎么能听模态关闭事件?

Spi*_*lot 6 modal-dialog addeventlistener ionic2

我需要知道我的模态何时被关闭.似乎没有明确的方法与ionViewDidLeave或类似的东西一样.我尝试了ionViewDidLeave并且感觉它不适用于模态关闭,因为我没有使用navController移动到那个页面,我正在使用模态显示页面.

home.ts

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

  postEvent(){ 

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


}
Run Code Online (Sandbox Code Playgroud)

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.  

    } 
Run Code Online (Sandbox Code Playgroud)

Aus*_*yke 11

你只需要听你家中的模态关闭事件

// 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();
}
Run Code Online (Sandbox Code Playgroud)

`

  • 在 Ionic 5 中,您需要使用 onDidDismiss().then(... (3认同)

Hay*_*ton 6

你会做

// 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();

}
Run Code Online (Sandbox Code Playgroud)

您可以在docs中找到它。


Jan*_*anP 6

这个问题是在 Ionic 4 发布之前提出的,但我认为这与这些天有关。离子 4 版本:

家.ts

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

添加事件.ts

constructor(public modalCtrl: ModalController) {}

close() {
    this.modalCtrl.dismiss({
        dismissvalue: value
    });
}
Run Code Online (Sandbox Code Playgroud)

并且不要忘记在 HomeModule 中包含 AddEventModule:

home.component.ts

@NgModule({
    declarations: [
        ...
    ],
    imports: [
        IonicModule,
        CommonModule,
        AddEventModule
    ],
    exports: [
        ...
    ]
})
Run Code Online (Sandbox Code Playgroud)