angularjs device back button not working

这一生的挚爱 提交于 2019-12-13 05:33:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!