2014-03-27 68 views
0

我現在用的是咆哮的角度指令時,得到一個循環依賴錯誤循環依賴:

Error: [$injector:cdep] Circular dependency found: spinnerInterceptor <- $http <- $compile http://errors.angularjs.org/1.2.11/$injector/cdep?p0=spinnerInterceptor%20%3C-%20%24http%20%3C-%20%24compile

angular.module('bedrock').factory('spinnerInterceptor', ['$q', 'growl', '$injector', function($q, growl, $injector) { 
    var count = 0; 

    function setCount(count) { 
    $('#spinner .count').text(count); 
    } 

    return { 
    request: function(config) { 
     /* jshint -W017 */ 
     if (!count++) { 
     // Start a spinner here. 
     $('#spinner').show(); 
     } 
     /* jshint +W017 */ 
     setCount(count); 

     return config || $q.when(config); 
    }, 
    response: function(response) { 
     if (!--count) { 
     // Stop the spinner here... 
     $('#spinner').hide(); 
     } 
     setCount(count); 

     return response || $q.when(response); 
    }, 
    responseError: function(rejection) { 
     if (!--count) { 
     // ...and here. 
     $('#spinner').hide(); 
     } 
     setCount(count); 
     var $compile = $compile || $injector.get('$compile'); 
     if(rejection.status != 401){ 
     growl.addErrorMessage($compile(
      '<h3>' + rejection.config.method + ' '+ rejection.config.url + '</h3>' + 
      '<hr><div>' + 
       (_.isObject(rejection.data) ? ('<pre>' + JSON.stringify(rejection.data, null, 2) + '</pre>') : rejection.data) + 
       '</div>' + '<a ng-click="reportError($event)"><i class="fa fa-bullhorn hoverable"></i> Report this error</a>'), 
      { 
      enableHtml: true 
      } 
     ); 
     } 

     return $q.reject(rejection); 
    } 
    }; 
}]).factory('timeoutInterceptor', function() { 
    return { 
    request: function(config) { 
     config.timeout = 20000; 
     return config; 
    } 
    }; 
}).config(['$httpProvider', function($httpProvider) { 
    $httpProvider.interceptors.push('spinnerInterceptor'); 
    $httpProvider.interceptors.push('timeoutInterceptor'); 
}]); 

如何解決這個問題?

回答

1

而是在你的攔截器注入$compile的,注入$injector並獲得$compile$injector.get('$compile')(而不是在攔截動初始化時間)。例如,你可以有一個關閉變量$compile,你在responseError方法中填寫一次。

+0

這似乎解決了循環依賴問題!然而,我現在正在收到一個問題,實際上我正試圖編譯HTML的消毒。我得到html.indexOf不是一個函數。 – user1200387

+0

你最後的問題可能會涉及返回'$ compile'的問題。 – lib3d