Angular Embed $ http in configuration or vendor to run

I am using angular -route-segment in my angular application and trying to configure segments from json feed.

I am having problems with this because I cannot figure out how to inject $http into the app.config function. This does not work with Unknown provider: $http

 myApp.config(["$http", "$routeSegmentProvider", function ($http, $routeSegmentProvider) { /* setup navigation here calling $routeSegmentProvider.when a number of times */ } 

So instead of entering $ http in config , I also tried entering $routeSegmentProvider in myApp.run

 myApp.run(["$http", "$routeSegment", function($http, $routeSegment) { /* can use $http here to get data, but $routeSegment is not the same here */ /* $routeSegment does not have the when setup method */ }]); 

I also tried

 myApp.run(["$http", "$routeSegmentProvider", function($http, $routeSegmentProvider) 

but I get an Unknown provider: $routeSegmentProviderProvider <- $routeSegmentProvider

+6
source share
1 answer

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

A small trick to get around this is to define a variable in the area of ​​the parent function, so that both the config and run blocks can access it:

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

I am not sure if you run into problems trying to call $ routeSegmentProvider.setup () at the start-up phase, as I have not tried. Previously, I was able to use the same technique for registering HTTP interceptors, which depend on some user services with $ httpProvider.

+11
source

Source: https://habr.com/ru/post/974749/


All Articles