angularjs - Angular JS $locationChangeStart 获取下一个 url 路由对象

标签 angularjs authorization angularjs-routing

我正在尝试在我的 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/

相关文章:

mysql - SQL 访问被拒绝

javascript - 重新调用 Controller 时丢失表单输入数据

javascript - 如何在 Angular JS 中加载页面?

javascript - 我需要使用 angularJS 将对象添加到数组中

javascript - 我如何使用 Angular Material 创建卡片网格?

javascript - 根据 AngularJS 中数组的大小更改文本

javascript - 使用 Angularjs $state.go 进行导航

javascript - ng-click 在指令模板内不起作用

mobile - Google API OAuth 2.0 Titanium : Required parameter is missing: response_type

node.js - PEM 例程 :PEM_read_bio:no start line - Unable to decode JWT token