Angular Inject $http into config or provider into run

后端 未结 1 1265
梦毁少年i
梦毁少年i 2020-12-31 18:28

I am using angular-route-segment in my angular app and an trying to configure the segments from a json feed.

I have having problems with this, because I can\'t figu

相关标签:
1条回答
  • 2020-12-31 19:05

    Providers can only be injected in the "config" phase and not the "run" phase. Conversely, a service such as $http will not have been initialized yet in the "config" phase and will only be available in the "run" phase.

    A little trick to work around this is to define a variable in the parent function scope so that both the "config" and "run" blocks can have access to it:

    var routeSegmentProvider = null;
    
    myApp.config(["$routeSegmentProvider", function ($routeSegmentProvider) {
        routeSegmentProvider = $routeSegmentProvider;
    }]);
    
    myApp.run(["$http", function($http) {
      // access $http and routeSegmentProvider here
    }]);
    

    I'm not sure if you will encounter problems trying to call $routeSegmentProvider.setup() within the run phase as I haven't tried. In the past I was able to use the same technique to register http response interceptors that depend on some custom services with the $httpProvider.

    0 讨论(0)
提交回复
热议问题