javascript AngularJS - 将提供程序注入 module.config

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19966013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:23:05  来源:igfitidea点击:

AngularJS - Inject provider to module.config

javascriptangularjs

提问by Alex Dn

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

我做错了什么?
根据文档,我应该能够注入providermodule.config......但我收到一个错误 - “未知提供者”

http://jsfiddle.net/g26n3/

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.providerto config of ab.coremodule.
2) How to inject the same provider (ab.core.provider) to config of abmodule.
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?

其实我在这里有三个问题......
1)如何注入模块的ab.core.provider配置ab.core
2) 如何将相同的提供者 ( ab.core.provider)注入ab模块的配置。
3)如果我将相同的提供者注入到两个模块的配置中,它将是提供者的同一个实例还是两个不同的实例?

Thank you!

谢谢!

回答by elclanrs

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

您需要添加“Provider”后缀,这就是 Angular 知道的,但就像 shaunhusain 在评论中所说的那样,有一些限制

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

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

遵循命名约定,使其看起来不错, .provider('camelCase', ...)