权限angularJS $ routeChangeStart和$ stateChangeSuccess

时间:2018-11-16 09:55:41

标签: javascript angularjs angular-ui-router

我正在尝试在有角JS中使用$routeChangeStart进行许可,但不起作用。我想做的是使$routeChangeStart$stateChangeSuccess一起工作:

angular.module('app').run(['$rootScope','$location', '$state', '$stateParams', 'loginService', 
  function($rootScope, $location, $state, $stateParams, loginService) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
    var routespermission=['/'];
    $rootScope.$on('$routeChangeStart', function(){     
        alert('testing');
        console.log('>>exit tab: '+routespermission.index0f($location.path()));
        console.log('>>logged:' +loginService.islogged());
        if(routespermission.index0f($location.path()) !=-1 && !loginService.islogged()){
            $location.path('/login');
        }
    });
    $rootScope.$on('$stateChangeSuccess', function() {
        window.scrollTo(0, 0);
        console.log('testing');
    });
    FastClick.attach(document.body);

  },
])

1 个答案:

答案 0 :(得分:0)

  

UI-Router,我想让代码可以在UI-Router上使用

使用$stateChangeStart代替$routeChangeStart

app.run(function($rootScope, $location, $state, $stateParams, loginService) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
    var routespermission=['/'];
    ̶$̶r̶o̶o̶t̶S̶c̶o̶p̶e̶.̶$̶o̶n̶(̶'̶$̶r̶o̶u̶t̶e̶C̶h̶a̶n̶g̶e̶S̶t̶a̶r̶t̶'̶,̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶(̶)̶{̶ ̶
    $rootScope.$on('$stateChangeStart', function(){     
        alert('testing');
        console.log('>>exit tab: '+routespermission.index0f($location.path()));
        console.log('>>logged:' +loginService.islogged());
        if(routespermission.index0f($location.path()) !=-1 && !loginService.islogged()){
            $location.path('/login');
        }
    });
    $rootScope.$on('$stateChangeSuccess', function() {
        window.scrollTo(0, 0);
        console.log('testing');
    });
    FastClick.attach(document.body);
})

有关更多信息,请参见UI-Router 0.3.1 Api Reference - $state


ui.router >= 1.0中不推荐使用StateChange事件。

有关更多信息,请参见UI Router stateChangeSuccess event not firing