Angular JS $ locationChangeStart获取下一个URL路由对象 [英] Angular JS $locationChangeStart get next url route object
问题描述
我正在尝试在我的角度应用程序上实现授权,当更改路线时,我想检查该路线是否已为用户授权.我尝试使用$routeChangeStart
,但是它不能阻止该事件.
I am trying to implement Authorization on my angular application, when a route is changed I want to check whether the route is authorized for user or not. I tried with $routeChangeStart
but it does not prevents the event.
我当前的代码:
$scope.$on('$routeChangeStart', function(event, next, current) {
if(current_user.is_logged_in){
var route_object = next.route_object;
if(!(route_object.route_roles)){
event.preventDefault();
}
}
});
在我的next
对象中,我正在获取在我的$routeProvider
Here in my next
object I am getting route_object which is set in my $routeProvider
var routes = object;
app.config(function($routeProvider) {
$routeProvider.when(url, {
templateUrl: "/users.html",
route_object: routes,
});
});
routes
是在我的函数中形成的对象,但是当我使用$locationChangeStart
时,我只是获得下一页和上一页的url,
routes
is an object which is formed in my function, but when I use $locationChangeStart
I am just getting url's of next and previous page,
如何获取整个路线对象?
How do I get the entire route object??
推荐答案
您可以像这样在$locationChangeStart
事件侦听器中获取route参数:
You can get the route parameter in an $locationChangeStart
event listener like this:
$scope.$on('$locationChangeStart', function(event, next, current) {
if(current_user.is_logged_in){
var route_object = ($route.routes[$location.path()]).route_object; //This is how you get it
if(!(route_object.route_roles)){
event.preventDefault();
}
}
});
然后经典的preventDefault方法将完成此工作.这是我为类似内容写的 plunker .
Then classic preventDefault method would do the work. Here's a plunker that I wrote for something similar.
这篇关于Angular JS $ locationChangeStart获取下一个URL路由对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!