AngularJs cannot inject $ provision and $ injection services in module.run

I am trying to define services dynamically in angularjs, as the docs say, $provide and $injector are services, so they should be entered in module.run . I need to make dynamic services available from the bootstrap of the application, so I'm trying to define them in module.run

 angular.module('remote.interface',[]) .run(['$provide', '$injector', function(provide, injector){ // provide dynamically }]); 

but it ends up with an error: [$injector:unpr] Unknown provider: $provideProvider <- $provide and the same eror for $injector if I try to remove $ include injection.
Where is the mistake?

[EDIT]

after some research, I tried something like this:

 var module = angular.module('remote.interface',[]) .run([function(){ var provide = module.provider(), injector = angular.injector(); provide.value('my.val',{i:'am a value'}); injector.get('my.val'); // this throws [$injector:unpr] Unknown provider: my.valProvider <- my.val }]); 

even if I delete the call to injector.get , if I try to enter my.val , for example, into the controller of another module, angular throws the same error.

+2
angularjs
Jul 12 '14 at 12:17
source share
1 answer

See the module documentation and read the comments in the installation example, especially these comments.

Config

Only suppliers (not instances) can be added to configuration blocks

Run

You can embed instances (not Providers) in startup blocks

Here is an example installation on JSFiddle to correctly insert $ provision and $ injector.

https://docs.angularjs.org/guide/module

 angular.module('myModule', []). config(function(injectables) { // provider-injector // This is an example of config block. // You can have as many of these as you want. // You can only inject Providers (not instances) // into config blocks. }). run(function(injectables) { // instance-injector // This is an example of a run block. // You can have as many of these as you want. // You can only inject instances (not Providers) // into run blocks }); 
+6
Jul 12 '14 at 15:31
source share



All Articles