Consider the following interceptor template for angular 1
(function () {
'use strict';
angular
.module('myApp')
.factory('apiError', apiError);
apiError.$inject = [
'$q',
'$injector',
'$http'];
function apiError($q,
$injector,
$http) {
}
})();
Injecting $q
and $injector
will not encounter any problem, but injecting $http
will cause circular dependency error.
Error: $injector:cdep
Circular Dependency
The fix I made is simply setting $http
using $injector
inline
var $http = $injector.get('$http');
But I have two questions
- Why is there circular dependency?
- How did
$injector
solved the circular dependency