Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

javascript - AngularJS - Inject provider to module.config

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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', ...)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...