【问题标题】:How can i enable device backbutton in ionic?如何在 ionic 中启用设备后退按钮?
【发布时间】:2018-04-05 02:28:35
【问题描述】:
我已经通过这样的后退按钮注册操作事件在某些条件下禁用了后退按钮:
$ionicPlatform.registerBackButtonAction(function (event) {
if (condition)
{
event.preventDefault();
$ionicHistory.nextViewOptions({ disableBack: true });
}
else
{
$ionicHistory.goBack();
}
}, 800);
那么现在我该如何再次启用该设备后退按钮?
因为它仍然被禁用并且也不会进入以前的视图。
【问题讨论】:
标签:
angularjs
ionic-framework
back-button
【解决方案1】:
你需要试试这个
var lastTimeBackPress = 0;
var timePeriodToExit = 2000;
platform.registerBackButtonAction(() => {
// get current active page
let view = this.nav.getActive();
if (view.component.name == "HomePage") {
//Double check to exit app
if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
platform.exitApp(); //Exit from app
} else {
let toast = this.toastCtrl.create({
message: 'Press back again to exit App',
duration: 3000,
position: 'bottom'
});
toast.present();
lastTimeBackPress = new Date().getTime();
}
} else {
// go to previous page
this.nav.pop({});
}
});
希望对你有用