如何刷新在AngularJS页面时重定向用户到登录页面 [英] How to redirect user to login page when refreshing the page in AngularJS
本文介绍了如何刷新在AngularJS页面时重定向用户到登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用我想,当他刷新他正在查看的页面将用户重定向到登录页面。我怎样才能做到这一点AngularJS?
In my application I want to redirect the user to the login page when he refreshes the page he is viewing. How can I do this in AngularJS?
推荐答案
您可以收听事件的 $ locationChangeStarted ,如果用户没有登录,prevent默认行为( preventDefault()
事件对象的方法),并改变 $地点到任何你想要的页面。
You may listen to event $locationChangeStarted and, if user not logged in, prevent default behavior (preventDefault()
method of event object) and change $location to any page you want.
下面是一个伪codeD例如:
Here is a pseudocoded example:
myModule.run(function($locaiton, $rootScope, MyLoginService) {
$rootScope.on('$locationChangeStarted', function(event) {
if (!MyLoginService.isAuthentificated() && event.oldUrl !== 'myloginpage') {
event.preventDefault();
$location.path('/myloginpage');
}
});
});
这篇关于如何刷新在AngularJS页面时重定向用户到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文