【发布时间】:2017-07-29 04:29:53
【问题描述】:
我在文档中试图找到一种方法来执行以下操作:
用户将在表单页面上,当他返回时,我需要像"are you sure you want to discard you alterations?" 那样进行验证,当他单击是或否时,它将执行一个功能,然后离开页面。
我知道我可以使用Platform.registerBackButtonAction() 来检查他何时单击硬件后退按钮,但我怎样才能对标题上的 NavControll 后退按钮执行相同操作?
我可以使用 NavGuards,它在 NavControll 后退按钮上自动实现,但它会离开页面,然后执行该功能。
所以我需要做的流程是:
enter page >> write something on any input >> if it tries to leave the page open the check alert >> on clicking 'yes' it leaves the page
这是我正在做的一段代码:
ionViewCanLeave() {
let a = this.alerts.create({
title: "Confirmation message?",
buttons: [{
text: 'Nop',
handler: () => {
this.navCtrl.pop();
}
}, {
text: 'Yes',
handler: () => {
this.salvarDescricao();
}
}]
});
if (this.changes != undefined && this.changes!= '') { //just a check i do, if the user doesn't change anything i don't need to ask
a.present();
} else {
this.navCtrl.pop(); //if nothing changes it pops twice
}
}
那么我该如何实现呢?如何防止用户离开视图、执行代码并依赖于它离开的结果?
谢谢:)
【问题讨论】:
标签: angular typescript ionic-framework ionic2