未捕获错误:[$ injector:cdep]找到循环依赖项:auth< - redirect< - $ http< - auth

时间:2016-07-31 05:52:26

标签: angularjs

angular.module('project_tracker')
        .config(['$locationProvider','$routeProvider','$provide',
            'authProvider','jwtInterceptorProvider','$httpProvider',
            function config($locationProvider, $routeProvider,$provide,
                authProvider,jwtInterceptorProvider,$httpProvider){
                authProvider.init({
                    domain: 'xxxxxxxxxxxxxx',
                    clientID: 'xxxxxxxxxxxxxx'
                });
                jwtInterceptorProvider.tokenGetter = function($localStorage){
                    return $localStorage.id_token;
                }
                $locationProvider.hashPrefix('!');
                $routeProvider.
                    when('/projects_list',{
                        template: '<project-list></project-list>'
                    }).
                    when('/building_add',{
                        template: '<building-add></building-add>'
                    }).
                    when('/project_view/:projectId',{
                        template: '<project-view></project-view>'
                    }).
                    when('/project_add',{
                        template: '<project-add></project-add>'
                    }).
                    when('/project_update/:projectId',{
                        template: '<project-update></project-update>'
                    }).
                    when('/profile', {
                        template : '<profile></profile>',
                    })
                function redirect($q,$injector,auth, $localStorage, $location){
                    return {
                        reponseError: function(rejection){
                            if(rejection.status === 401){
                                auth.signout();
                                delete $localStorage.profile;
                                delete $localStorage.id_token;
                                delete $localStorage.projects;
                                delete $localStorage.projects;
                                delete $localStorage.project_number;
                                $location.path('/home');           
                            }
                            return $q.reject(rejection);
                        }
                    }
                }
                $provide.factory('redirect',redirect);                 
                $httpProvider.interceptors.push('redirect');
                $httpProvider.interceptors.push('jwtInterceptor');
            }
            ])
        .run(function($rootScope,auth,$localStorage,jwtHelper,$location){
            $rootScope.$on('$locationChangeStart',function(){
                var token = $localStorage.id_token;
                if(token){
                    if(!jwtHelper.isTokenExpired(token)){
                        if(!auth.isAuthenticated){
                            auth.authenticate($localStorage.id_token,token);
                        }
                    }
                }
                else {
                    $location.path('/home');
                }
            })
        })

我正在关注这些tutorials,在我添加重定向功能后出现此错误:

Circular dependency found: auth <- redirect <- $http <- auth
                http://errors.angularjs.org/1.5.7/$injector/cdep?p0=auth%20%3C-%20redirect%20%3C-%20%24http%20%3C-%20auth

我是新手,我对此并不了解。

1 个答案:

答案 0 :(得分:5)

$httpauth(服务或工厂)之间存在循环依赖关系。

你应该做的不是直接DI auth,而是在重定向拦截器中使用$injector

 var auth= $injector.get('auth');