AngularJS - 将提供程序注入 module.config [英] AngularJS - Inject provider to module.config

查看:32
本文介绍了AngularJS - 将提供程序注入 module.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做错了什么?
根据文档,我应该能够将 provider 注入到 module.config...但我收到一个错误 - Unknown Provider"

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']);

}());

其实我这里有三个问题...
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?

谢谢!

推荐答案

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

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) {
    ...
  }]

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

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

这篇关于AngularJS - 将提供程序注入 module.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆