Angular JS $locationChangeStart get next url route object

后端 未结 4 2105
忘掉有多难
忘掉有多难 2021-02-14 08:44

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 $ro

4条回答
  •  时光取名叫无心
    2021-02-14 08:59

    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();
            }
        }
    });
    

    Then classic preventDefault method would do the work. Here's a plunker that I wrote for something similar.

提交回复
热议问题