AngularJs在初始化之前加载所有文件
AngularJs load all files before initialization
我遇到nomod Module not available错误。
现在我的情况是,在开发过程中,我的文件可能会以随机顺序加载(由于使用了grunt工具注入器)。所以我可以在foo.module.js
中声明模块,在foo.js
中声明实现。我可能首先加载foo.js
。
foo.js
angular.module('Foo').config( //...
foo.module.js
angular.module('Foo', [
'ngRoute'
// etc....
]);
我的html页面的简化版本是:
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
</head>
<body>
<div id="app-wrapper" class="app" ng-view></div>
<!-- [injector:js] -->
<script src="/app/foo.js"></script>
<script src="/app/foo.module.js"></script>
<!-- [endinjector] -->
<script>
angular.element(document).ready(function() {
angular.bootstrap(document, ["Foo"]);
});
</script>
</body>
</html>
注意[injector:js]
标签。该工具将扫描源目录,并添加在此标记之间找到的所有javascript文件的引用。构建的另一个过程将获取这些文件,对其进行合并和缩小(此示例中省略了其标记)。
因此,如果我在开发环境中(现在的html)或在构建后(concat+minification)运行它。。。我最终会出现以下错误:
Uncaught Error: [$injector:nomod] Module 'Foo' 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
有没有任何方法可以防止angular在实际加载之前执行依赖于模块的方法?我无法重新安排订单。我不能使用require.js或任何AMD,因为这已经绑定到构建工具中了。
通常我使用usemin-grumt插件和include-source的组合。
这允许我从自动包含中提取模块文件,但仍然可以对其进行绑定、注释和缩小。
index.html
最终看起来像这样:
<!-- build:js js/app.js -->
<script src="js/app.js"></script>
<!-- include: "type": "js", "files": "js/controllers/**/*.js" -->
<script src="js/controllers/someController.js"></script>
...
... lots of scripts here ...
...
<!-- /include -->
<!-- endbuild -->
为了你的目的,它值得一看。
相关文章:
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- Ajax文件加载和<输入>文件加载
- 使用Bootstrap将JSON文件加载到表中
- 将组合JSON文件加载到谷歌地图
- 将文本文件加载到javascript中
- jQuery Mobile样式从另一个文件加载内容
- 使用JavaScript将压缩文本文件加载到字符串中
- 数组在手动写入时有效,但从文本文件加载时无效
- 如何在打印前等待javascript文件加载twitter,并在打印后关闭选项卡
- 从文本文件加载jsondata时出现空白页
- AngularJS:如何使用ng-include将javascript文件加载到部分中
- 使用jquery将html文件加载到html中,用于离线/本地项目
- 从另一个grunt文件加载grunt任务
- 将php文件加载到弹出窗口中,并将php变量发布到该弹出窗口中(使用onclick)
- 如何使用node.js将不同的json文件加载到不同的json对象中
- 将变量从服务器上的外部文件加载到 HTML 文档中
- 节点 - 将 JSON 文件加载到代码中
- 如何使用 JavaScript/jQuery 将 XML 文件加载到 JSP/HTML 页面
- NodeJS和ExpressJS-Javascript文件加载,但CSS文件不加载;t
- D3.js:通过Force Layout从JSON文件加载多个网络