2015-06-08 72 views
0

我正在使用angular.js并尝试从http拦截器内访问当前路由。如果请求的路由具有自定义的authorize属性并且用户没有登录(我知道这不是对后端授权的替代,它旨在提供更好的用户体验),则目标是将用户重定向到登录页面。拦截未经授权的路由请求并重定向

$routeProvider 
     .when('/profile', { authorize: true, templateUrl: 'page-profile.html' }); 


    $httpProvider.interceptors.push(function($rootScope, $q, $location) { 
      return { 
       request: function (config) { 

        var $route = what here? seems i cannot inject $route 

        if($route.authorize && !$rootScope.user) { 
         $location.path("/sign-in"); 
        } 

        return config; 
       } 
      }; 
     }]); 

我尝试使用$route服务,但由于某些原因,应用程序犯规引导,如果我尝试将其注入拦截。即使我能够,我也需要解决方案来使用使用ui-router并暴露$ state而不是$ route的Ionic框架来使用移动设备。

如何从拦截器访问路由对象,而不依赖$ route(它似乎无法工作)?

采用了棱角分明1.4

回答