我如何在ionic2 (v 2.1.7
)关闭模态形式后调用方法?以下是我的工作流程
- 用户加载页面(页面a)
- 然后点击按钮打开模态表单(页B)
- 添加一些细节(这将更新一个服务)
- 关闭模态表单(B页)
- 现在我想添加一个回调到页a和读取新的值从服务
这就是我所做的
#page A
let modal = this.modalCtrl.create(NewListPage);
modal.present();
getValueFromService()
#page B
updateService()
this.viewCtrl.dismiss();
目前发生的事情是,一旦程序击中modal.present();
,它不等待Page B
关闭之前,它去getValueFromService()
,由于这一点,新更新的值不能读取一旦我关闭模态形式。
您可以像这样使用onDidDismiss
(doc):
// Page A
presentModal() {
let modal = this.modalCtrl.create(NewListPage);
modal.onDidDismiss(() => {
// This is going to be executed when the modal is closed, so
// you can read the value from the service here
getValueFromService();
});
modal.present();
}
然后就像你说的
// Page B
updateService();
this.viewCtrl.dismiss();
还请注意,你可以在Page A
和Page B
之间发送数据,所以也许你甚至可以避免使用服务(如果你只是为了来回发送数据),只是像这样发送数据:
// Page A
presentModal() {
let modal = this.modalCtrl.create(NewListPage);
modal.onDidDismiss(dataFromModal => {
// You can use the data here
console.log(dataFromModal.foo);
});
modal.present();
}
在B页
// Page B
// Instead of calling the service, you can send the data to the caller
let data = { 'foo': 'bar' };
this.viewCtrl.dismiss(data);