简体   繁体   中英

angularjs device back button not working

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.

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; }); }]); 

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; }); }]); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM