ui.router REDIRECTION - 在toState

时间:2016-03-21 18:20:32

标签: angularjs redirect angular-ui-router state

未经过身份验证的用户将重定向到登录页面。

$rootScope.$on('$stateChangeStart', function(event, toState)
正如预期的那样,

toState正在运行两次,但最后一次运行是目标运行而不是登录页面数据。请参阅下面的代码。

指数:

<section ng-if="page.header" ng-class="{'transparent-header': page.transparentHeader}" user-header></section>

控制器:

.controller('appController', ['$scope','$state','$rootScope', function($scope,$state,$rootScope){

    //Control header and footer views getting data from router
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){

        $scope.page = toState.data;

    })




}]);

重定向:

.run(['$rootScope', '$state', 'userService', '$sessionStorage', function($rootScope, $state, userService, $sessionStorage){

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

    //Detecting sessions
    if($sessionStorage.userData){
        var goodORnot = $sessionStorage.userData.isLogged;
    }else{
        goodORnot = false;
    }

    var isAuthenticationRequired =  toState.data 
          && toState.data.requiresLogin 
          //&& !userService.isLogged
          && !goodORnot
      ;

    if(isAuthenticationRequired)
    {
      event.preventDefault();
      $state.go('LoginPage');
    }
  });
}]);

控制台日志:

Object {name: "Login", header: false, footer: false, css: false, transparentHeader: false}

Object {name: "Your Profile", header: true, footer: false, css: true, transparentHeader: true…}

如何阻止 toState 再次运行?如果重定向发生,我不希望控制器中的toState运行,或者我如何覆盖它?

0 个答案:

没有答案