admin管理员组文章数量:1294342
In my AngularJS application I'm redirecting the route
to a specific page when the user isn't logged. To do that I'm using a variable on $rootScope
.
Now I would like to prevent the browser's back button when the user is logged. I would like to redirect it to a specific page (the registration
view). The problem is I don't know if there's a back button event.
My code is:
angular.module('myApp',[...]
//Route configurations
}])
.run(function($rootScope, $location){
$rootScope.$on('$routeChangeStart', function(event, next, current){
if(!$rootScope.loggedUser) {
$location.path('/register');
}
});
$rootScope.$on('$locationChangeStart', function(event, next, current){
console.log("Current: " + current);
console.log("Next: " + next);
});
});
So on $locationChangeStart
I would write a pseudocode like:
if (event == backButton){
$location.path('/register');
}
Is it possible?
A naive solution would be writing a function that checks if next
and current
are in the wrong order, detecting if the user is going back.
There are other solutions? I'm approaching the problem in a wrong way?
In my AngularJS application I'm redirecting the route
to a specific page when the user isn't logged. To do that I'm using a variable on $rootScope
.
Now I would like to prevent the browser's back button when the user is logged. I would like to redirect it to a specific page (the registration
view). The problem is I don't know if there's a back button event.
My code is:
angular.module('myApp',[...]
//Route configurations
}])
.run(function($rootScope, $location){
$rootScope.$on('$routeChangeStart', function(event, next, current){
if(!$rootScope.loggedUser) {
$location.path('/register');
}
});
$rootScope.$on('$locationChangeStart', function(event, next, current){
console.log("Current: " + current);
console.log("Next: " + next);
});
});
So on $locationChangeStart
I would write a pseudocode like:
if (event == backButton){
$location.path('/register');
}
Is it possible?
A naive solution would be writing a function that checks if next
and current
are in the wrong order, detecting if the user is going back.
There are other solutions? I'm approaching the problem in a wrong way?
Share Improve this question edited Oct 30, 2013 at 17:24 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Oct 30, 2013 at 14:49 AtropoAtropo 12.6k8 gold badges50 silver badges62 bronze badges 2- What is it that you are trying to achieve? Why block the back button if the route will be redirected? – Brian Lewis Commented Oct 30, 2013 at 15:11
- I want to block the default behaviour of the back button, redirecting it to the first page of my application, when the user is logged. – Atropo Commented Oct 30, 2013 at 15:22
1 Answer
Reset to default 6I found a solution, which is easier than I thought. I register on a object in $rootScope
the actual location and on every location change I check with the new one. In this way I can detect if the user is going back in the history.
angular.module('myApp',[...], {
//Route configurations
}])
.run(function($rootScope, $location) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
if(!$rootScope.loggedUser) {
$location.path('/register');
}
});
$rootScope.$on('$locationChangeSuccess', function() {
$rootScope.actualLocation = $location.path();
});
$rootScope.$watch(function() { return $location.path() },
function(newLocation, oldLocation) {
if($rootScope.actualLocation == newLocation) {
$location.path('/register');
}
});
});
});
本文标签: javascriptAngularJS redirect a route only on browser39s back buttonStack Overflow
版权声明:本文标题:javascript - AngularJS redirect a route only on browser's back button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741583543a2386710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论