【问题标题】:AngularJS - Injecting a ProviderAngularJS - 注入提供者
【发布时间】:2014-01-19 19:26:34
【问题描述】:

编辑:使用 YEOMAN 搭建我的应用程序,

我有以下 YEOMAN 生成的提供者

'use strict';

angular.module('myApp')
  .provider('myProvider', function () {

    // Private variables
    var salutation = 'Hello';

    // Private constructor
    function Greeter() {
      this.greet = function () {
        return salutation;
      };
    }

    // Public API for configuration
    this.setSalutation = function (s) {
      salutation = s;
    };

    // Method for instantiating
    this.$get = function () {
      return new Greeter();
    };
  });

我正在尝试将它注入到我的应用配置中,如下所示:

'use strict';

angular.module('myApp', [
        'ngRoute',
        'myProvider'
    ])
    .config(function ($routeProvider, myProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'views/main.html',
                controller: 'MainCtrl'
            })
            .otherwise({
                redirectTo: '/'
            });
    });

我收到以下错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module lpSocialApp due to:
Error: [$injector:modulerr] Failed to instantiate module myProvider due to:
Error: [$injector:nomod] Module 'myProvider' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

我错过了什么?

【问题讨论】:

标签: javascript angularjs dependency-injection provider


【解决方案1】:

我可以在您的代码中看到两个错误:

1) 您不能在您的应用程序模块中注入“myProvider”,因为“myProvider”是在您的应用程序模块中定义的。

2) 'myProvider' 的名称错误,Angular 会自动将 'Provider' 附加到您的提供者。

这里是修复:

1) 在专用模块中定义您的提供程序,并将这个新模块添加到您的应用程序模块依赖项中。

2) 将您的提供程序重命名为“my”(或在您的配置函数中注入“myProviderProvider”):

angular.module('myProviderModule', [])
    .provider('my', function () { // or 'myProvider'

        // Private variables
        var salutation = 'Hello';

        // Private constructor
        function Greeter() {
            this.greet = function () {
                return salutation;
            };
        }

        // Public API for configuration
        this.setSalutation = function (s) {
          salutation = s;
        };

        // Method for instantiating
        this.$get = function () {
            return new Greeter();
        };
    });

angular.module('myApp', [
    'ngRoute',
    'myProviderModule'
])
.config(function ($routeProvider, myProvider) { // or 'myProviderProvider'
    $routeProvider
        .when('/', {
            templateUrl: 'views/main.html',
            controller: 'MainCtrl'
        })
        .otherwise({
            redirectTo: '/'
        });
});

看到这个小提琴:http://jsfiddle.net/Z3k2s

【讨论】:

  • 感谢您的回复,我可以看到这可以解决我的问题,但是我正在使用 YEOMAN 生成代码,并希望相信他们正在使用那里的最佳实践......可能是他们的生成器中的错误?
  • @DavinTryon 'myProvider'这个名字没有错,但是AngularJS在注入时会自动将'Provider'附加到config函数中的参数。解决方法是去掉'Provider'后缀(myProvider -> my)或者在配置函数中添加 'Provider' 后缀('myProvider' -> 'myProviderProvider')。
【解决方案2】:

您将提供者与模块混淆了。模块包括一组提供者、服务和工厂。

你也不应该在定义提供者时添加提供者后缀,否则你必须像myProviderProvider一样注入它。

另外,您似乎混淆了angular.module 的语法:

// create a new module foo.bar with dependency to ngRoute
angular.module('foo.bar', ['ngRoute']);

// create a new module woo.hoo with NO dependency
angular.module('woo.hoo', []); 

// get already created module foo.bar
angular.module('foo.bar')

您的代码已修复:

'use strict';

angular.module('someModule', [])
  .provider('my', function () {

    // Method for instantiating
    this.$get = function () {
      return {}// return something
    };
  });



'use strict';

angular.module('myApp', [
        'ngRoute',
        'someModule'
    ])
    .config(function ($routeProvider, myProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'views/main.html',
                controller: 'MainCtrl'
            })
            .otherwise({
                redirectTo: '/'
            });
    });

【讨论】:

  • or else you have to inject it like myProviderProvider。我不确定这是不是真的。 Angular 会在末尾添加“Provider”(在注入器中),但 myProvidermyProviderProvider 在使用提供程序时都应该可以工作。
  • 感谢您的回复,我可以看到这可以解决我的问题,但是我正在使用 YEOMAN 生成代码,并希望相信他们正在使用那里的最佳实践......可能是他们的生成器中的错误?
  • @DavinTryon 否。在配置阶段,您必须将其注入为myProviderProvider,但在运行阶段,您可以将其注入为myProvider,但它不再是提供者,这将是一个具有提供者名称的服务,这会产生误导。
  • @ShlomiSchwartz 我不知道有关 yeoman 的最新消息,但它为您提供了生成的代码供您开始编码。你应该改变它。
  • @UmurKontacı 虽然这是正确的,但 yeoman 输出应该按原样工作,这样您就有了一个已知良好的基础。所以,是的,我同意这是一个错误。
【解决方案3】:

您正在尝试加载名为 myProvider 的依赖 模块

angular.module('myApp', [
        'ngRoute',
        'myProvider'
    ])

myProvider 已经在myApp 模块中,所以你可以这样做:

angular.module('myApp', [
        'ngRoute'
    ])
    .config(function ($routeProvider, myProvider) {

【讨论】:

  • 谢谢,我不能让它工作......你能添加一个 JSFiddle 吗?
猜你喜欢
  • 2016-02-18
  • 2015-06-13
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 2022-10-05
  • 2021-06-23
  • 2021-04-23
  • 1970-01-01
相关资源
最近更新 更多