2013-08-06 32 views
0

我创建使用angularjs 1.1.5Angularjs历史+ PhoneGap的注销回

在应用程序运行时移动的PhoneGap应用程序时,将显示一个登录表单。成功登录后,用户将被重定向到新的'/accounts/profile'url(ProfileCtrl控制器)。

在每个页面上(位于'/accounts/login'的登录表单旁边),位于屏幕左上方的后退按钮 。

整个应用程序绑定到“AppCtrl”控制器(以及提到的后退按钮的顶栏)。应用程序的其余部分绑定到一个指令,每个指令都有独立的控制器。

返回按钮是通过简单地返回window.history.back()

我需要禁用简档页面(成功登录后所显示的一个),位于“/账户上的window.history.back() /简档在AppCtrl控制器定义的函数“”绑定到ProfileCtrl控制器。 反而使用用户应该注销。 (为了简单起见,我在此省略注销确认)。这同样适用于打电话的后退按钮。

目前,我改变从$scope.$parent.goBack() = logout()子范围goBack()功能ProfileCtrl ..但我不知道,我怎么会绑定到物理后退按钮。

回答

0

绑定到物理后退按钮,你可以使用:

document.addEventListener("backbutton", $scope.goBack, false); 

要做到这一点,你需要在你的控制器使用组合角$窗口和$位置服务。我建议不要覆盖你的$父范围函数,因为你可能在另一个页面上出现奇怪的行为。另外,请注意,您不允许在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早ondeviceready:navigator.connection.type not working even if device is ready *or* device is never ready