AngularJS - 注入提供商module.config [英] AngularJS - Inject provider to module.config
问题描述
我在做什么错了?结果
根据文件,我应该能够在提供商
注入到 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"
(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']);
}());
其实我有三个问题在这里...结果
1)如何注入 ab.core.provider
到 ab.core
模块的配置。结果
2)如何注入相同的提供者( ab.core.provider
)以 AB
模块的配置。结果
3)如果我将注入相同的供应商,这两个模块的配置,这将是供应商的同一个实例或者这将是两个不同的实例?
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?
感谢您!
推荐答案
您需要添加的提供者的后缀,这是怎么知道角,但像shaunhusain在评论中说,有<一个href=\"http://stackoverflow.com/questions/12903338/angularjs-dependency-injection-of-value-inside-of-module-config\">some限制的:
You need to add the "Provider" suffix, that's how Angular knows, but like shaunhusain said in the comments there are some limitations:
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) {
...
}]
请按照命名约定,所以它看起来不错, .provider('驼峰',...)
Follow naming conventions so it looks good, .provider('camelCase', ...)
这篇关于AngularJS - 注入提供商module.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!