Javascript AngularJS在引导后添加依赖项

Javascript AngularJS在引导后添加依赖项,javascript,angularjs,dependency-injection,bootstrapping,Javascript,Angularjs,Dependency Injection,Bootstrapping,我想在AngularJS启动后添加依赖项。我试图通过app.requires.push('app.main')来实现这一点如本文所建议()。但是,它不起作用 下面是我的示例代码: index.html <!DOCTYPE html> <html ng-app="app"> <head> <link rel="stylesheet" href="style.css"> <script type="text/javascri

我想在AngularJS启动后添加依赖项。我试图通过app.requires.push('app.main')来实现这一点如本文所建议()。但是,它不起作用

下面是我的示例代码:

index.html

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <link rel="stylesheet" href="style.css">
    <script type="text/javascript" src="https://code.angularjs.org/1.4.3/angular.min.js"></script>
    <script src="script.js"></script>

  </head>
  <body>
    <h1>Hello Plunker!</h1>
  </body>

</html>
脚本2.js

alert("it's loaded");
angular.module('app.main', [])
.run(function(){
  alert("it's running");
});
console.log(app);
app.requires.push('app.main');


为什么它不起作用?如何修复它?

moduleName.requires
没有文档记录,理解不透彻,而且不可靠。反过来,在引导过程中(通过
ng app
angular.bootstrap
)或使用
angular.Injector
创建新的喷油器时,会调用喷油器

一旦应用程序启动且配置/运行阶段结束,就无法调用新的配置/运行块。同样的关注点是
moduleName.directive
moduleName.controller
和其他模块方法,所有这些都应该在应用程序启动之前调用。因此,它们不适合异步定义模块

可以调用新定义的运行块(这意味着创建了一个新的应用程序实例):

它的主要用途是测试,在生产中的应用有限——新实例化的应用程序及其服务无法与引导应用程序通信,因为服务单例不同


对于Angular中的延迟加载,有几种解决方案,其中最全面的是。

您认为这为什么不起作用@lukkea如果它正在工作,它应该显示
它正在运行
警报框。我的怀疑是。当你通过requires.add添加时,运行不会发生(当你加载脚本时,应用程序已经运行);您是否尝试将服务添加到app.main并从app调用该服务?(我意识到这可能不适合您的解决方案,但至少它会让您了解app.main是否确实正确加载以供从app使用。
alert("it's loaded");
angular.module('app.main', [])
.run(function(){
  alert("it's running");
});
console.log(app);
app.requires.push('app.main');
var newApp = angular.injector(['app.main']);