Angular Inject $http into config or provider into run

一个人想着一个人 提交于 2019-11-30 09:22:42

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!