Is there a way to request $http for an interceptor?

后端 未结 2 1015
后悔当初
后悔当初 2020-12-10 19:41

The idea is to get a data from another source in certain cases, so I have this stub:

factory(\"interceptor\", function ($q, $location, $http) {
    return fu         


        
2条回答
  •  悲&欢浪女
    2020-12-10 19:58

    Inject $injector to interceptor:

    Use it to get $http inside the returned object within callback functions.

    Here is an example

    app.config(function ($httpProvider) {
      $httpProvider.interceptors.push('interceptor');
    });
    
    app.factory("interceptor", function ($q, $location, $injector) {
      return {
        request: function(config){      
          var $http = $injector.get('$http');
          console.dir($http);
          return config;
        }
      }
    });
    
    app.run(function($http){
      $http.get('/')
    });
    

提交回复
热议问题