我是使用AngularJs和PhoneGap的App dev的新手,所以我可能在这里做错了(请纠正)。我的目标是当/ viewInitialDepartments处于活动状态时(即当用户在该视图中时),如果有人在Android中按下BackButton,则退出App。相关守则如下: -
在 index.html 页面中: -
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width,height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" >
<meta http-equiv="x-ua-compatible" content="ie=10">
<meta name="msapplication-tap-highlight" content="no" >
<link rel="stylesheet" href="css/module-reset.css" ><link rel="stylesheet" href="css/module-grid.css" ><link rel="stylesheet" href="css/module-all.css" ><link rel="stylesheet" href="css/module-transitions.css"><link rel="stylesheet" href="css/module-custom.css" > <link rel="stylesheet" href="css/styleMenu.min.css">
<title>ad</title>
<script type="text/javascript" src="cordova.js"></script>
</head>
<body ng-cloak ng-app="askDadaApp">
<div id="mainBodyWrapper">
<div class="page {{ pageClass }}" ng-view></div>
</div>
<script src="js/angular.min.js"></script>
<script src="js/angular-route.js"></script>
<script src="js/angular-animate.js"></script>
<script src="js/angular-touch.js"></script>
<script src="js/ngprogress.min.js"></script>
<link rel="stylesheet" href="css/ngProgress.css" >
<script src="js/script-controller.js"></script>
</body>
</html>
我的Angular JS Contol页面( script-controller.js ): -
var adApp = angular.module('adApp',['ngRoute', 'ngAnimate','ngTouch','ngProgress']);
adApp.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {templateUrl: 'views/1-setconnection.html',controller: 'setController' })
.when('/viewInitialDepartments', {templateUrl: 'views/2-viewInitialDepartments.html',controller: 'viewInitialDepartmentsController' })
.when('/offLine', { templateUrl: 'views/1-offLine.html',controller:'offLineController'})
});
// CONTROLLERS
adApp.controller('offLineController', function($scope, $http, $location, $routeParams ) { $scope.pageClass = 'normal';var notOnlineToast = document.getElementById("notOnlineToast");notOnlineToast.style.display="block";
});
adApp.controller('setController', function($scope, $http, $location) {
var typeList = "CHECKCONNECTION";
$http.get("URL/php/fetchData.php?typeList=" + typeList )
.error(function(response) {$location.path('/offLine');
// if not offline then go to Home View (viewInitialDepartments)
$location.path('/viewInitialDepartments');
});
adApp.controller('viewInitialDepartmentsController', function($scope, $http, $location, $routeParams) {
var typeList = "CHECKCONNECTION";
$http.get("URL/php/fetchData.php?typeList=" + typeList )
.error(function(response) {
$location.path('/offLine');
});
// REST OF THE CODE
});
我的 config.xml 文件包含声明:-gap:plugin name =“org.apache.cordova.dialogs”
现在的问题是,当用户在/ viewInitialDepartments视图中时,我需要知道如何在Android Phone上按BackButton时退出App。 请帮忙。 (其他一切都按预期工作正常。)
编辑 - 我想这可以使用document.addEventListener(“deviceready”,onDeviceReady,false)来实现;和document.addEventListener(“backbutton”)但不知道如何在我现在的代码中这样做,以便backButton执行它对所有其他视图的操作,只是在/ viewInitialDepartments视图中使应用程序关闭。
答案 0 :(得分:0)
定义全局变量以存储当前路径(视图)名称
var CURRENT_ROUTE;
在路线更改时将值分配给CURRENT_ROUTE
var adApp = angular.module('adApp',['ngRoute', 'ngAnimate','ngTouch','ngProgress']).run([
'$rootScope',
function ($rootScope) {
$rootScope.$on('$routeChangeStart', function (event, next) {
CURRENT_ROUTE = next;
});
}]);
对后退按钮执行操作
document.addEventListener("backbutton", function(){
if(CURRENT_ROUTE === yourviewname){
navigator.app.exitApp(); // exit app
}
}, false);