我正在尝试在我的 Angular 应用程序上实现授权,当路线更改时,我想检查该路线是否已为用户授权。我尝试使用 $routeChangeStart
但它不会阻止该事件。
我当前的代码:
$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
中设置的route_object
var routes = object;
app.config(function($routeProvider) {
$routeProvider.when(url, {
templateUrl: "/users.html",
route_object: routes,
});
});
routes
是在我的函数中形成的一个对象,但是当我使用 $locationChangeStart
时,我只是获取下一页和上一页的 url,
如何获取整个路由对象?
最佳答案
您可以在 $locationChangeStart
事件监听器中获取路由参数,如下所示:
$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我为类似的事情写的。
关于angularjs - Angular JS $locationChangeStart 获取下一个 url 路由对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26674714/