AngularJS仅在浏览器的后退按钮上重定向路由
AngularJS redirect a route only on browser's back button
在我的AngularJS应用程序中,当我没有记录用户时,我将route
重定向到特定页面。为此,我在$rootScope
上使用了一个变量。
现在我想在登录用户时阻止浏览器的后退按钮。 我想将其重定向到特定页面(registration
视图)。问题是我不知道是否有后退按钮事件。
我的代码是:
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);
});
});
所以$locationChangeStart
我会写一个伪代码,比如:
if (event == backButton){
$location.path('/register');
}
可能吗?
一个天真的解决方案是编写一个函数来检查next
和current
的顺序是否错误,检测用户是否要返回。
还有其他解决方案吗?我以错误的方式处理问题?
我找到了一个解决方案,这比我想象的要容易。我在$rootScope
实际位置的对象上注册,并在每个位置更改时与新位置进行检查。通过这种方式,我可以检测用户是否正在返回历史记录。
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');
}
});
});
});
相关文章:
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 我想重定向点击,然后更改按钮文本,我该怎么做
- 单击转盘按钮后重定向到另一个页面
- 如何在重定向到asp.net中单击按钮的下一页之前应用javascript警报
- 如何在单击按钮重定向页面时显示加载程序gif
- 如果答案正确,如何将提交按钮重定向到下一页
- 在同一功能上使用多个按钮重定向到不同的功能
- 根据单击的提交按钮重定向到php页面
- 类似AJAX的按钮重定向我,而不是执行AJAX调用
- 如何停止浏览器转发按钮重定向到页面
- 按钮重定向不起作用
- 通过单击按钮重定向到jQuery中的另一个网页
- 贝宝网站支付专业Iframe -支付与贝宝按钮重定向到贝宝
- 清除字段按钮(重定向不起作用)
- 从注销按钮重定向后,站点不加载索引样式
- 滚动到顶部按钮重定向到主页
- 在PayPal按钮重定向之前完成AJAX调用
- ngDialog关闭按钮重定向到其他页面
- Ajax按钮重定向在Selenium webdrivers上
- 后退按钮重定向脚本