本文介绍了AngularJS:注射服务为一体的HTTP拦截器(循环依赖)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个HTTP拦截我的AngularJS应用程序来处理身份验证。

I'm trying to write a HTTP interceptor for my AngularJS app to handle authentication.

这code的工作,但我很担心,因为我以为角度应该自动处理手动注射服务:

This code works, but I'm concerned about manually injecting a service since I thought Angular is supposed to handle this automatically:

    app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push(function ($location, $injector) {
        return {
            'request': function (config) {
                //injected manually to get around circular dependency problem.
                var AuthService = $injector.get('AuthService');
                console.log(AuthService);
                console.log('in request interceptor');
                if (!AuthService.isAuthenticated() && $location.path != '/login') {
                    console.log('user is not logged in.');
                    $location.path('/login');
                }
                return config;
            }
        };
    })
}]);

我开始了在做,但遇到了循环依赖问题:

What I started out doing, but ran into circular dependency problems:

    app.config(function ($provide, $httpProvider) {
    $provide.factory('HttpInterceptor', function ($q, $location, AuthService) {
        return {
            'request': function (config) {
                console.log('in request interceptor.');
                if (!AuthService.isAuthenticated() && $location.path != '/login') {
                    console.log('user is not logged in.');
                    $location.path('/login');
                }
                return config;
            }
        };
    });

    $httpProvider.interceptors.push('HttpInterceptor');
});

为什么我担心的另一个原因是,在$ HTTP 在角文档的部分似乎显示方式来获得依赖注入的正规途径成为一个Http拦截。请参阅拦截器的code片断:

Another reason why I'm concerned is that the section on $http in the Angular Docs seem to show a way to get dependencies injected the "regular way" into a Http interceptor. See their code snippet under "Interceptors":

// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
  return {
    // optional method
    'request': function(config) {
      // do something on success
      return config || $q.when(config);
    },

    // optional method
   'requestError': function(rejection) {
      // do something on error
      if (canRecover(rejection)) {
        return responseOrNewPromise
      }
      return $q.reject(rejection);
    },



    // optional method
    'response': function(response) {
      // do something on success
      return response || $q.when(response);
    },

    // optional method
   'responseError': function(rejection) {
      // do something on error
      if (canRecover(rejection)) {
        return responseOrNewPromise
      }
      return $q.reject(rejection);
    };
  }
});

$httpProvider.interceptors.push('myHttpInterceptor');

以上code应该去哪里?

Where should the above code go?

我想我的问题是什么是去这样做的正确方法?

I guess my question is what's the right way to go about doing this?

谢谢,我希望我的问题是清楚。

Thanks, and I hope my question was clear enough.

推荐答案

您有$ HTTP和你AuthService之间循环依赖。

You have a circular dependency between $http and your AuthService.

正在使用 $注射器服务被延迟的AuthService的$ HTTP的依赖性解决鸡和蛋的问题做什么。

What you are doing by using the $injector service is solving the chicken-and-egg problem by delaying the dependency of $http on the AuthService.

我相信,你做了什么其实是做的最简单的方法。

I believe that what you did is actually the simplest way of doing it.

您也可以通过这样做:

这篇关于AngularJS:注射服务为一体的HTTP拦截器(循环依赖)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!