简体   繁体   English

如何从Angular.js中的另一个模块注入提供程序

[英]How to inject provider from another module in Angular.js

Why this example not work? 为什么这个例子不起作用? jsfiddle I get error what provider not found. jsfiddle我得到错误提供者没找到。

var m1 = angular.module('m1', [])
    .provider('test', function() {
        return {
            $get: function() {
                return 'Hello from provider';
            }
        }
     });

var m2 = angular.module('m2', ['m1'])
    .config(['test', function(test) {
        alert(test);
    }]);

In the config function you do not have access to it, try using the run method. config功能中,您无权访问它,请尝试使用run方法。

var m2 = angular.module('m2', ['m1'])
  .run(['test', function(test) {
    alert(test);
  }]);

What you DO have access to in the config function is the service-provider for test so you could do 您在config功能中可以访问的是用于test的服务提供商,因此您可以这样做

var m2 = angular.module('m2', ['m1'])
  .config(['testProvider', function(test) {
    alert(test);
  }]);

This would typically be if you want to provide some sort of configuration to your test-service that would be specific for the m2 module. 如果您希望为测试服务提供某种特定于m2模块的配置,通常会这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM