13

What I'm doing wrong?
According to documentation, I should be able to inject the provider to module.config...but I'm getting an error - "Unknown Provider"

http://jsfiddle.net/g26n3/

(function () {
    "use strict";

    angular.module("ab.core", [])
        .provider("ab.core.provider", function () {
            console.log("ab.core.provider - constructor");
            this.$get = function () {
                console.log("ab.core.provider - get");
                return { value: "test" };
            }
        })
        .config(["ab.core.provider", function (myProvider) { console.log("ab.core - config " + myProvider.value); }])
        .run(function () { console.log("ab.core - run"); });

    angular.module("ab", ["ab.core"])
        .config(["ab.core.provider", function () { console.log("ab - config"); }])
        .run(function () { console.log("ab - run"); });

    angular.bootstrap(document, ['ab']);

}());

Actually I have three questions here...
1) How to inject the ab.core.provider to config of ab.core module.
2) How to inject the same provider (ab.core.provider) to config of ab module.
3) If I will inject the same provider to config of both modules, it will be the same instance of provider or it will be two different instances?

Thank you!

Alex Dn
  • 5,465
  • 7
  • 41
  • 79
  • possible duplicate of [AngularJS dependency injection of value inside of module.config](http://stackoverflow.com/questions/12903338/angularjs-dependency-injection-of-value-inside-of-module-config) – shaunhusain Nov 13 '13 at 22:51
  • Hi I just voted to close, it's a well laid out question but the problem I believe is in the premise and is answered by the fact that you can't inject certain providers into config as explained in the SO post I linked. – shaunhusain Nov 13 '13 at 22:52
  • @shaunhusain I saw that post...but it didn't help me, since I had a different problem. I didn't tried to inject a "value" or "service" as it was in question you linked. – Alex Dn Nov 13 '13 at 23:14
  • fair enough I just wasn't entirely understanding the code but had seen the problem about injecting providers into config, glad you got it resolved. – shaunhusain Nov 14 '13 at 07:54

1 Answers1

31

You need to add the "Provider" suffix, that's how Angular knows, but like shaunhusain said in the comments there are some limitations:

http://jsfiddle.net/g26n3/1/

angular.module("ab.core", [])
  .provider("ab.core.provider", function () {

  })
  .config(["ab.core.providerProvider", function(p) {
    ...  
  }]

angular.module("ab", ["ab.core"])
  .config(["ab.core.providerProvider", function(p) {
    ...
  }]

Follow naming conventions so it looks good, .provider('camelCase', ...)

Community
  • 1
  • 1
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • Thank you, now it works, somehow I missed the "suffix" part :) – Alex Dn Nov 13 '13 at 23:10
  • 3
    You got undefined in the fiddle. here is the fix. http://jsfiddle.net/g26n3/37/. At the config state , you still have full access to the provider methods without the $get filtration yet. – Royi Namir Dec 26 '15 at 17:02