问题
I am trying to create dialog when device back button clicked, searched internet and came up with the coding. 1.when i use the below coding, Clicking back button shows dialog ok and cancel when i click ok:the app closes. 2. when i click cancel: the app closes.
app.run(['$rootScope','$mdDialog','$cordovaDialogs', function($rootScope, $mdDialog, $cordovaDialogs) {
document.addEventListener("deviceready", function() {
console.log("deviceready");
document.addEventListener("backbutton", onBackKeyDown,false);
function onBackKeyDown() {
if(confirm("Are You sure You wanna Exit?")){
console.log("true");
navigator.app.exitApp();
}else{
return false;
}
}
},false);
$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
$rootScope.title = current.$$route.title;
});
}]);
Please look into the above coding, i am doing anything wrong.. ? I just need to show confirm dialog inside device ready.. And cancel must not end the app.. Thank you in advance guys.
回答1:
sry, I have changed the code like this then also the same the app closes for both true and false of confirm..
app.run(['$rootScope','$mdDialog','$cordovaDialogs', function($rootScope, $mdDialog, $cordovaDialogs) {
document.addEventListener("deviceready", function() {
console.log("deviceready");
document.addEventListener("backbutton", onBackKeyDown,false);
function onBackKeyDown() {
if(confirm("Are You sure You wanna Exit?")){
console.log("true");
navigator.app.exitApp();
}else{
return false;
}
}
},true);
$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
$rootScope.title = current.$$route.title;
});
}]);
回答2:
Thanks for the support guys, I have completed this issue by getting help from my mates.. Please see the code below.. :)
app.run(['$rootScope','$location', function($rootScope,$location) {
document.addEventListener("deviceready", function() {
console.log("deviceready");
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
e.preventDefault();
if ($location.path() === "/login" || $location.path() === "/home") {
var r=confirm("exit");
if(r==true){
console.log("not exit");
navigator.app.exitApp();
}else {
navigator.app.goBack();
}
}else {
/* $ionicHistory.goBack(); */
window.history.back();
navigator.app.goBack();
}
}
}, 100);
$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
$rootScope.title = current.$$route.title;
});
}]);
来源:https://stackoverflow.com/questions/35331734/angularjs-device-back-button-not-working