permission angularJS $routeChangeStart and $stateChangeSucce



我试图在angular 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);
},
])

UI路由器,我想让代码可以在UI路由器上使用

使用$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路由器0.3.1 Api参考-$state。


StateChange事件已被ui.router >= 1.0否决。

有关详细信息,请参阅UI Router stateChangeSuccess事件未启动。

最新更新