title: AngularJS - What is the advantage of using $injector in this case?
body: <p>I was reading the following article: <a href="https://medium.com/opinionated-angularjs/techniques-for-authentication-in-angularjs-applications-7bbf0346acec" rel="nofollow">Techniques for authentication in AngularJS applications</a> when I saw this implementation:</p>
  
  <pre><code>.config(function ($httpProvider) {
    $httpProvider.interceptors.push([
      '$injector',
      function ($injector) {
        return $injector.get('AuthInterceptor');
      }
    ]);
  })
  .factory('AuthInterceptor', function ($rootScope, $q,
                                        AUTH_EVENTS) {
    return {
      responseError: function (response) { 
        $rootScope.$broadcast({
          401: AUTH_EVENTS.notAuthenticated,
          403: AUTH_EVENTS.notAuthorized,
          419: AUTH_EVENTS.sessionTimeout,
          440: AUTH_EVENTS.sessionTimeout
        }[response.status], response);
        return $q.reject(response);
      }
    };
  })
  </code></pre>
  
  <p>Could someone please explain me why does the author use an injector here instead of:</p>
  
  <pre><code>.config(function ($httpProvider) {
    $httpProvider.interceptors.push({
      responseError: function (response) { 
        $rootScope.$broadcast({
          401: AUTH_EVENTS.notAuthenticated,
          403: AUTH_EVENTS.notAuthorized,
          419: AUTH_EVENTS.sessionTimeout,
          440: AUTH_EVENTS.sessionTimeout
        }[response.status], response);
        return $q.reject(response);
      });
  })
  </code></pre>
  
  <p>I would also appreciate if someone could explain to me why does he writes this:</p>
  
  <pre><code>'$injector',
  </code></pre>
  
  <p>before the function.</p>
  
  <p>Thanks you all very much!</p>