2013-08-06 31 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