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?
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.
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');
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.