angularjs分布式模块未加载

angularjs分布式模块未加载,angularjs,Angularjs,为这个新手问题和可能显而易见的答案道歉。我曾尝试将我的应用程序分解成更小的文件,并在运行时使用angular的模块加载语法将其包括在内。谢谢你的帮助,如果这个问题不符合要求,我再次道歉 我得到的错误是: Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:modulerr] Failed to instantiate module eventBus du

为这个新手问题和可能显而易见的答案道歉。我曾尝试将我的应用程序分解成更小的文件,并在运行时使用angular的模块加载语法将其包括在内。谢谢你的帮助,如果这个问题不符合要求,我再次道歉

我得到的错误是:

Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module eventBus due to:
Error: [$injector:nomod] Module 'eventBus' is not available! You either misspelled the modu...<omitted>...1) 
和eventBus.js:

var myApp = angular.module('myApp');

myApp.factory("eventBus", function ($rootScope) {
  var eventBus = {};
  eventBus.message = '';

  eventBus.prepForBroadcast = function(msg) {
    this.message = msg;
    this.broadcastItem(msg);
  };

  eventBus.broadcastItem = function(msg) {
    console.log('eventBus message: ' + eventBus.message);
    $rootScope.$broadcast('handleBroadcast', msg );
  };

  return eventBus;
});

您的应用程序正在声明对名为eventBus的模块的依赖关系,但您正在通过工厂将eventBus定义为服务。模块仅依赖于其他模块。如果希望以这种方式将eventBus作为模块依赖项,则需要将eventBus脚本更改为如下所示:

var myEventBusModule = angular.module('eventBus', []);

myEventBusModule.factory("eventBusService", function ($rootScope) {
  var eventBus = {};
  eventBus.message = '';

  eventBus.prepForBroadcast = function(msg) {
    this.message = msg;
    this.broadcastItem(msg);
  };

  eventBus.broadcastItem = function(msg) {
    console.log('eventBus message: ' + eventBus.message);
    $rootScope.$broadcast('handleBroadcast', msg );
  };

  return eventBus;
});
您的“eventBus”只是“myApp”的一个工厂。不是一个独立的模块

试试这个:

eventBus.js

var module = angular.module('eventBus', []);
module.factory('eventBus', ["$rootScope", function($rootScope){
   // Content of your factory...
}]);

或者简单地从应用程序模块的依赖项中删除“eventBus”

非常感谢,我没有意识到这一点。我感谢你的迅速反应。您的答案似乎要求我更改所有引用,如下所示:$scope.eventBus=eventBus;到$scope.eventBus=eventBusService?这当然是一个小变化。但我喜欢遵守命名约定,你的回答意味着按照某种约定在名称中添加“服务”。如果你想使用单独的模块,那么可以。如果你所要做的只是将代码放在不同的文件中(我建议使用模块,但这取决于你),那么你所需要做的就是从应用程序的依赖项中删除你没有创建的模块。有道理吗?是的,谢谢。我将使用单独的模块,我可以在不将代码中的“eventBus”更改为“eventBusService”的情况下使用这些模块,但我可以按照惯例这样做。谢谢,致以最良好的问候!BrianThank感谢您的时间和专业知识!向你问好,布莱恩
var myEventBusModule = angular.module('eventBus', []);

myEventBusModule.factory("eventBusService", function ($rootScope) {
  var eventBus = {};
  eventBus.message = '';

  eventBus.prepForBroadcast = function(msg) {
    this.message = msg;
    this.broadcastItem(msg);
  };

  eventBus.broadcastItem = function(msg) {
    console.log('eventBus message: ' + eventBus.message);
    $rootScope.$broadcast('handleBroadcast', msg );
  };

  return eventBus;
});
var module = angular.module('eventBus', []);
module.factory('eventBus', ["$rootScope", function($rootScope){
   // Content of your factory...
}]);