2016-11-13 95 views
1

如何在離子模式2(v 2.1.7)中關閉模態窗體後調用方法?以下是我的工作流程模態窗體關閉時的回調

  • 用戶加載一個頁面(A)
  • 然後點擊一個按鈕,打開一個模式窗體(頁B)
  • 添加一些細節(這將更新服務)
  • 關閉模式窗體(頁B)
  • 現在我想一個回調添加到頁面A,從 讀取新的價值服務

這是世界衛生大會T I做過

#page A 
let modal = this.modalCtrl.create(NewListPage); 
modal.present(); 
getValueFromService() 

#page B 
updateService() 
this.viewCtrl.dismiss(); 

目前發生的事情是,一旦程序碰到modal.present();,它不是等待Page B它去getValueFromService()之前關閉,由於這一點,最近更新的價值不能被讀取一次我關閉模式形式。

任何幫助,將不勝感激

歡呼

回答

1

您可以使用onDidDismissdoc)是這樣的:

// 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 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); 
+1

謝謝隊友,完美的作品 – sameera207

+0

很高興聽到:) – sebaferreras