如何将所需模块的服务注入模块配置功能?

sta*_*son 2 javascript angularjs

我有一个单独的服务模块,我需要一个服务注入到依赖模块的配置功能.

angular.module('app', ['app.services'], function(myservice) {/*use myservice*/})
  .controller('mycontroller', function($scope, myservice) {
    $scope.message = myservice.sayHello();
  });

angular.module('app.services', [], function($provide) {
  $provide.service('myservice', function() {
    return {sayHello: function() { return 'hello'; }};
  });
});
Run Code Online (Sandbox Code Playgroud)

我也创造了一个小提琴:http://jsfiddle.net/FzGmL/

代码将与应用程序中的未知提供程序:myservice爆炸

如果myserviceapp模块配置函数中删除参数,mycontroller构造函数能够myservice注入就好了.

如何myservice注入app模块的配置功能?

Ben*_*esh 6

您正在寻找Module.run()方法,它在注入完成加载后为您完成初始化工作.

angular.module('myApp', ['app.services'])
   .run(function(myservice) {
      //stuff here.
   });
Run Code Online (Sandbox Code Playgroud)