Angularjs+phonegap注销历史记录
Angularjs + phonegap logout on history back
我使用angularjs 1.1.5 创建了一个移动电话间隙应用程序
在应用程序运行时,会显示一个登录表单。在成功登录之后,用户被重定向到新的"/accounts/profile"url(ProfileCtrl控制器)。
在每个页面上(位于"/accounts/login"的登录表单旁边),都有一个后退按钮位于屏幕左上角。
整个应用程序都绑定到"AppCtrl"控制器(以及上面提到的带有后退按钮的顶栏)。应用程序的其余部分与ng-view指令绑定,每个指令都有单独的控制器。
后退按钮是AppCtrl控制器中定义的一个功能,只需返回window.history.back()
我需要禁用配置文件页面(成功登录后显示的页面)上的window.history.back()
,该页面位于绑定到ProfileCtrl控制器的"/accounts/profile"。相反,用户应该注销。(为了简单起见,我省略了注销确认)。这同样适用于按下手机的后退按钮。
目前,我正在使用ProfileCtrl中的$scope.$parent.goBack() = logout()
从子作用域更改goBack()
函数。。但我不知道,我该如何将它绑定到物理后退按钮。
要绑定到物理后退按钮,可以使用:
document.addEventListener("backbutton", $scope.goBack, false);
要做到这一点,您需要在控制器中使用Angular$window和$location服务的组合。我建议不要覆盖$parent-scope函数,因为您可能会在另一个页面上出现奇怪的行为。此外,要知道您不允许在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 early ondeviceready:navigator.connection.type即使设备准备好也不工作*或*设备从未准备好
- window.location替换并传递URL历史记录条目中的变量
- Javascript历史记录转换为php行
- 浏览器何时记录历史记录
- 输入类型按钮返回历史记录并返回顶部
- HTML5历史记录-返回上一个完整页面按钮
- Angular JS-如何强制浏览器记录历史以便快速路由
- asp.net网站文件下载历史记录
- 使用Ajax的后退按钮历史记录,URL中没有散列
- 历史记录和通过JavaScript修改的HTML
- Javascript:历史记录未加载页面
- 通过js设置页面历史记录和页面内容
- 如果没有历史记录,我如何隐藏图像或按钮onclick=;历史.go(-1);
- 将HTML表单发布到iframe会导致浏览器历史记录出现问题
- 如何防止浏览器在历史记录中缓存HTML
- 如何将新的stateObject添加或附加到历史记录中
- 检查浏览器后退按钮历史记录
- React-router 在添加历史记录后不会渲染组件
- 如何返回可用的历史记录状态数
- 引导选项卡不适用于历史记录.返回选项
- 非用户发起的操作触发的导航没有记录历史