Javascript Angularjs+;phonegap在返回历史记录时注销
我已经使用angularjs 1.1.5创建了一个mobile phonegap应用程序 在应用程序运行时,将显示一个登录表单。成功登录后, 用户被重定向到新的“/accounts/profile”url(ProfileCtrl控制器) 在每个页面上(位于“/accounts/login”的登录表单旁边),都有一个后退按钮 位于屏幕左上角 整个应用程序都绑定到“AppCtrl”控制器(以及上面提到的带有后退按钮的topbar)。应用程序的其余部分绑定到一个ng view指令,每个指令都有单独的控制器 后退按钮是在AppCtrl控制器中定义的一个函数,只需返回Javascript Angularjs+;phonegap在返回历史记录时注销,javascript,angularjs,cordova,Javascript,Angularjs,Cordova,我已经使用angularjs 1.1.5创建了一个mobile phonegap应用程序 在应用程序运行时,将显示一个登录表单。成功登录后, 用户被重定向到新的“/accounts/profile”url(ProfileCtrl控制器) 在每个页面上(位于“/accounts/login”的登录表单旁边),都有一个后退按钮 位于屏幕左上角 整个应用程序都绑定到“AppCtrl”控制器(以及上面提到的带有后退按钮的topbar)。应用程序的其余部分绑定到一个ng view指令,每个指令都有单独的控
window.history.back()
我需要禁用配置文件页面(成功登录后显示的页面)上的窗口.history.back()
,该页面位于绑定到ProfileCtrl控制器的“/accounts/profile”。
相反,用户应该注销。(为了简单起见,我省略了注销确认)。这同样适用于按下手机的后退按钮
目前,我正在使用
$scope.$parent.goBack()=logout()
在ProfileCtrl中更改子作用域的goBack()
函数。。但我不知道如何将其绑定到物理后退按钮。要绑定到物理后退按钮,可以使用:
document.addEventListener("backbutton", $scope.goBack, false);
为此,您需要在控制器中结合使用$window和$location服务。我建议不要重写$parent作用域函数,因为您可能会在另一个页面上出现奇怪的行为。另外,要知道,不允许您在iOS设备上退出或挂起应用程序。请参见下面的一些示例代码:
var AppCtrl = ['$scope', '$window', '$location', '$timeout', '$notification', '$rootScope', function ($scope, $window, $location, $timeout, $notification, $rootScope) {
'use strict';
// Somewhere inside AppCtrl
$scope.checkExit = function () {
// https://stackoverflow.com/questions/14422908/iphone-does-not-recognize-phonegaps-navigator-app
// fack!!!
if ($location.path() == '/home' && !$scope.isIOS) {
$notification.confirm("Exit application?", function(result) {
if ($window.isPhoneGap && result == 1) {
$rootScope.$broadcast('appExit'); // ga tracking
navigator.app.exitApp();
}
});
return true;
}
return false;
};
$scope.goBack = function (evt) {
if (evt != null) {
if (evt.preventDefault) {
evt.preventDefault();
}
}
if (!$scope.checkExit()) {
$window.history.back();
}
};
document.addEventListener("backbutton", $scope.goBack, false);
}]; // End AppCtrl
// in some other file, I have $notification friendly factory
app.factory('$notification', ['$rootScope', '$window', function ($rootScope, $window) {
return {
alert: function(message) {
if (!$window.isPhoneGap) {
$window.alert(message);
return;
}
navigator.notification.alert(message, null, '', 'OK');
},
confirm: function (message, callbackFn, title, buttonLabels) {
if (buttonLabels == null) {
buttonLabels = 'OK,Cancel';
}
if (!$window.isPhoneGap) {
callbackFn($window.confirm(message) ? 1 : 2);
return;
}
navigator.notification.confirm(
message, // message
callbackFn, // callback to invoke with index of button pressed
title, // title
buttonLabels.split(',') // buttonLabels
);
},
prompt: function (message, callbackFn, title, defaultText, buttonLabels) {
if (buttonLabels == null) {
buttonLabels = 'OK,Cancel';
}
if (defaultText == null) {
defaultText = '';
}
if (!$window.isPhoneGap) {
var answer = $window.prompt(message, defaultText);
callbackFn({
buttonIndex: (answer ? 1 : 2),
input1: answer
});
return;
}
navigator.notification.prompt(
message, // message
callbackFn, // callback to invoke
title, // title
buttonLabels.split(','),
defaultText
);
}
};
}]);
在下面的代码示例中,我将window.isPhoneGap提前设置为ondevicerady: