【发布时间】:2014-09-13 22:00:26
【问题描述】:
我正在为我的 Phonegap/Angular 应用程序覆盖 Android/WP8 的后退按钮行为。几周前我已经实现了它,它就像一个魅力。但是,由于我犯了一个存储库错误,那部分代码丢失了。所以今天我一直在重新创建它,但无论我做什么,我都无法在 MainController 中访问我的 handleBack() 函数。
在我的 index.html 中,我在标题中包含了所有各种 .js 文件,这是我的正文:
<body ng-view ng-class="slide" ng-controller="MainController" >
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady(event){
document.addEventListener("backbutton", handleBackButton, false);
}
function handleBackButton(event){
alert("entered handleBackButton");
angular.element('[ng-controller=MainController]').scope().handleBack(event);
}
</script>
</body>
这是我的主控制器:
.controller('MainController', ['$scope', '$rootScope', '$window', '$location', 'currentLocale', 'Auth',
function ($scope, $rootScope, $window, $location, currentLocale, Auth) {
$scope.slide = '';
$rootScope.back = function() {
$(window).blur();
$scope.slide = 'slide-right';
if (typeof (navigator.app) !== "undefined") {
navigator.app.backHistory();
} else {
$window.history.back();
}
}
$rootScope.go = function(path, direction) {
$(window).blur();
$scope.slide = typeof direction !== 'undefined' ? direction : 'slide-left';
$location.url(path);
}
$rootScope.login = function() {
$scope.slide = 'slide-right';
$location.path('/');
$window.localStorage.clear();
}
$rootScope.logout = function() {
if(navigator.notification.confirm("Are you sure you want to logout?",
function(index){
if(index == 1){
Auth.logout(function(data) {
$rootScope.login();
});
}
},
"Confirm")) {
Auth.logout(function(data) {
$rootScope.login();
});
}
}
$rootScope.showAlert = function(message, title, labels, callback) {
var bl = (labels) ? labels : currentLocale.OK;
var t = (title) ? title : "";
var cb = (callback) ? callback : function(){};
navigator.notification.alert(
message,
cb,
t,
bl
);
}
$scope.handleBack = function(e){
alert("back button tapped");
if ( $location.url() == "/" ) {
if (navigator.notification.confirm("Are you sure you want to exit?")) {
navigator.app.exitApp();
}
else {
return false;
}
}
else if( $location.url() == "/home" ) {
$rootScope.back();
$window.localStorage.clear();
}
else{
$rootScope.back();
}
}
}])
我收到“输入 handleBackButton”警报,但它没有在 $scope.handleBack() 中输入警报。
我做错了什么????
【问题讨论】:
标签: javascript android angularjs cordova windows-phone-8