2

Is there a way to get provider instance / object at run time using string? I can get factory that way using:

var injector  = angular.injector(["myApp", 'ng']);
var keyf1 = injector.get('keyF1');  //-- providing it exists

but if done for provider, injector.get seems to return new instance of provider; that causes issue as there is statefull data in provider, so new instance is not what I need - I need access to current instance using string name.

My guess is that it could be worked around by putting statefull data into config part of provider, but I am not sure that is the best approach here .

Aurelio
  • 24,702
  • 9
  • 60
  • 63
Zoran Krunic
  • 197
  • 1
  • 2
  • 12

1 Answers1

0

Providers are used to configure factories in config time, before Angular.js application starts running. We should use them to configure how factories are going to behave in runtime. So, I think you need to do some hack to access a provider on runtime.

You may try to change your approach. You may put your stateful data to a factory. I can try to do a better comment, if you can give a concrete example.

Also, this is a good comparison: https://stackoverflow.com/a/15666049/158523

Community
  • 1
  • 1
Umut Benzer
  • 3,476
  • 4
  • 36
  • 53
  • Yes that is an option - I use factories for few things in the code - but was hoping to keep providers in cases where config part provides flexibility I don't get from factories and I might need. But yes, that is another option - get away from provider, or dynamic referencing using name. – Zoran Krunic Jun 05 '15 at 15:38
  • .. it is just interesting that one can get reference to factory instance at run time but not to providers. – Zoran Krunic Jun 05 '15 at 15:39