在app.js中使用工厂时出现角度“错误:$ injector:cdep循环依赖关系" [英] Angular 'Error: $injector:cdep Circular Dependency' when using factory in app.js

查看:286
本文介绍了在app.js中使用工厂时出现角度“错误:$ injector:cdep循环依赖关系"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用令牌来处理我的用户身份验证.我在路上遇到了颠簸,但我不确定从这儿去哪里.我环顾四周,看起来应该使用$injector服务,但是我不确定100%.谁能帮忙吗?

I am trying to use tokens to handle my user authentication. I hit a bump in the road and I am not entirely sure where to go from here. I looked around a bit, and it looks like I should use the $injector service, but I am not 100% sure. Could anyone please assist?

我创建了一个从节点获取令牌的工厂:

I created a factory that gets the token from node:

angular.module('myApp').factory('authenticateInterceptor', function(authenticateToken){

var authenticateInterceptorFactory = {};

authenticateInterceptorFactory.request = function(config){

    var token = authenticateToken.getToken();

    if(token){
        config.headers['x-access-token'] = token;
    };

    return config;
}

return authenticateInterceptorFactory;

});

这是 autenticateToken 的代码:

angular.module('myApp').factory('authenticateToken', function($http, $window){

    authenticateTokenFactory = {};

    authenticateTokenFactory.getToken = function(token){
        return $window.localStorage.getItem('token');
    };

    return authenticateTokenFactory;

});

这里没有错误,当我尝试在我的 app.js 中使用此工厂时,就会出现问题.

There are no errors here, the problem comes in when I try to use this factory in my app.js.

angular.module('myApp', [
    'dependancies goes here'
])
.config(function($httpProvider){
    $httpProvider.interceptors.push('authenticateInterceptor');
});

这现在会导致错误,我似乎无法将工厂传递到拦截器中.

This now causes the error, I can't seem to pass my factory into the interceptors.

推荐答案

循环依赖很可能是由于authenticateToken工厂内部注入$http服务引起的

Most likely the circular dependency is caused by the injection of $http service inside authenticateToken factory

之所以存在冲突,是因为在引导阶段角度尝试解决$http依赖性(如以及其他核心服务).

the conflict exists because at bootstrap phase angular try to resolve $http dependencies (as well as other core services) in this order.

  1. $httpProvider依赖项
  2. authenticateInterceptor依赖项
  3. authenticateToken依赖项(包括$http,在这里我们回到1.)
  1. $httpProvider dependencies
  2. authenticateInterceptor dependencies
  3. authenticateToken dependencies (include $http, and here we go back to 1.)

顺便说一句,由于authenticationFactory中未使用$http服务,因此您甚至可以删除注入,但是如果您需要该服务,则可以仅通过避免这种行为尝试动态注入它.

By the way, since the $http service is not used in authenticationFactory you can even remove the injection, but if you need the service you can try injecting it dynamically only by need to avoid this behaviour.

angular.module('myApp').factory('authenticateToken', function($injector, $window){

    authenticateTokenFactory = {};

    authenticateTokenFactory.getToken = function(token){
        return $window.localStorage.getItem('token');
    };

   authenticateTokenFactory.otherMethod = function(){
    //this code is not reached when your interceptor is added to 
    //$httpProvider interceptors array so theorically neither the exception
     var http = $injector.get('$http');
     http.get (/)...

   }

    return authenticateTokenFactory;

});

这篇关于在app.js中使用工厂时出现角度“错误:$ injector:cdep循环依赖关系"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆