Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>The Fix</h2> <p>Use the <code>$injector</code> service to get a reference to the <code>$state</code> service.</p> <pre><code>var interceptor = ['$location', '$q', '$injector', function($location, $q, $injector) { function success(response) { return response; } function error(response) { if(response.status === 401) { $injector.get('$state').transitionTo('public.login'); return $q.reject(response); } else { return $q.reject(response); } } return function(promise) { return promise.then(success, error); } }]; $httpProvider.responseInterceptors.push(interceptor); </code></pre> <h2>The Cause</h2> <p><a href="https://github.com/angular-ui/ui-router" rel="noreferrer">angular-ui-router</a> injects the <code>$http</code> service as a dependency into <code>$TemplateFactory</code> which then creates a circular reference to <code>$http</code> within the <code>$httpProvider</code> itself upon dispatching the interceptor.</p> <p>The same circular dependency exception would be thrown if you attempt to inject the <code>$http</code> service directly into an interceptor like so.</p> <pre><code>var interceptor = ['$location', '$q', '$http', function($location, $q, $http) { </code></pre> <h2>Separation of Concerns</h2> <p>Circular dependency exceptions can indicate that there is a mixing of concerns within your application which could cause stability issues. If you find yourself with this exception you should take the time to look at your architecture to ensure you avoid any dependencies that end up referencing themselves. </p> <h2>@Stephen Friedrich's answer</h2> <p>I agree with the answer below that using the <code>$injector</code> to directly get a reference to the desired service is not ideal and could be considered an anti pattern. </p> <p>Emitting an event is a much more elegant and also decoupled solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload