Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genbliz/e7ae527a5edce70764a4 to your computer and use it in GitHub Desktop.
Save genbliz/e7ae527a5edce70764a4 to your computer and use it in GitHub Desktop.
Avoid AngularJS circular dependency error when using $state in a HTTP interceptor
angular
.module('app')
.factory('AuthInterceptor', AuthInterceptor);
function AuthInterceptor($injector, $q) {
var $state;
var service = {
responseError: responseError
};
return service;
function responseError(rejection) {
$state = $state || $injector.get('$state');
if (rejection.status === 401 && rejection.config.url !== '/login') {
$state.go('login');
}
return $q.reject(rejection);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment