ionic 双击退出

1.首选要在界面引入 ng-cordova.js 或者 ng-cordova.min.js
2.将ngCordova注入到主模块中

   angular.module('starter',['ionic','ngCordova',............(后面代码省略)

3.安装提示插件

 cordova plugin add https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git

4.代码

.run(function ($ionicPlatform, $rootScope, $location, $timeout, $ionicHistory, $cordovaToast) {  
$ionicPlatform.ready(function ($rootScope) {       // for form inputs)   
 if (window.cordova && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);  
  }   
 if (window.StatusBar) {      // org.apache.cordova.statusbar required   
   StatusBar.styleDefault();   
 }  }); 
 //双击退出  
$ionicPlatform.registerBackButtonAction(function (e) {    //判断处于哪个页面时双击退出   
 if ($location.path() == '/tab/news') {    
  if ($rootScope.backButtonPressedOnceToExit) {    
      ionic.Platform.exitApp();   
   } else {       
      $rootScope.backButtonPressedOnceToExit = true;  
      $cordovaToast.showShortTop('再按一次退出系统');         
      setTimeout(function () {   
         $rootScope.backButtonPressedOnceToExit = false;    
    }, 2000);} 
   }   
 else if ($ionicHistory.backView()) {  
      $ionicHistory.goBack();   
 } else {   
      $rootScope.backButtonPressedOnceToExit = true;   
      $cordovaToast.showShortTop('再按一次退出系统');  
    setTimeout(function () {        
       $rootScope.backButtonPressedOnceToExit = false;  
    }, 2000);    }  
    e.preventDefault();   
    return false;
  }, 101);})

你可能感兴趣的:(ionic 双击退出)