2014-09-01 82 views
1

我想知道一個人如何會通過限制訪問任何和所有的意見去(除登錄/註冊)的意見,直到問題的用戶登錄英寸限制訪問的意見,直到登錄

然後,我會用$ http.post將表單內容發佈到我的後端,如果用戶沒有問題登錄,則重定向到短劃線視圖。

+0

關注此鏈接我認爲這將幫助你:https://medium.com/opinionated-angularjs/techniques-for-authentication-in-angularjs-applications-7bbf0346acec – 2014-09-01 16:41:37

回答

2

我會建議使用$stateChangeStart事件偵聽修改意見,並做了檢查,如果用戶登錄或沒有。由於Ionic使用ui-router而不是Angular的默認$route,因此您需要使用ui-router的API。

https://angular-ui.github.io/ui-router/site/#/api/ui.router.state。$狀態

$rootScope.$on('$stateChangeStart', 
function(event, toState, toParams, fromState, fromParams){ 
    event.preventDefault(); 

    // Check if user is logged in here, if not redirect state 
    if (!User.valid) { 
     $state.go('home.login'); 
    } 
}); 

使用聲明的國家的決心功能要求你添加一個決心要保護的任何途徑。使用事件偵聽器可以讓你在一個地方處理它。

0

可以解決重定向如果沒有登錄

  config(['$routeProvider', function ($routeProvider) { 

      $routeProvider 
      .when('/restricted',{ 
      'templateUrl': //some template url, 
      //Etc... 
      //then ... 
      'resolve': { 
      'onEnter': function (redirectService, authService) { 

       if (!!!authService.isLogged()) { 

       redirectService.redirectNotlogged(); 
       } 
      } 
      } 
      }); 

     }]);