在 Angular js 的不同文件中使用多个模块
Working with multiple modules in different files in Angular js
你好,请原谅我的英语。
我的问题是这样的:我是 Angular js 的初学者(目前正在学习 1.4 版),我想将我的应用程序分为两个模块,如下所示:
应用.js
(function(){
var app = angular.module('store', ['store-products', 'ngAnimate', 'ui.bootstrap']);
app.controller('StoreController', function(){
...
});
app.controller('ReviewController', function(){
...
};
this.rateOnClick = function(){
...
};
this.addReview = function(product){
...
};
});
.
.
.
})();
产品.js
(function(){
var app = angular.module('store-products', []);
app.directive('productTitle', function(){
...
});
app.directive('productPanels', function(){
...
});
})();
索引.html
<html>
<head>
<title>My Angular App!</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-animate.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.2.4.js"></script>
<link href="styles.css" rel="stylesheet">
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="app.js"></script>
<script src"products.js"></script>
...
</html>
我的所有文件都在同一个文件夹中.app.js是主模块,所以据我了解,包含很好,但我仍然收到错误:
Uncaught Error: [$injector:modulerr] Failed to instantiate module store due to:
Error: [$injector:modulerr] Failed to instantiate module store-products due to:
Error: [$injector:nomod] Module 'store-products' 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.
有人想不通为什么会这样吗?
哈哈,刚刚想通,问题是这行:
<script src"products.js"></script>
像这样修复:
<script src="products.js"></script>
您需要在应用脚本之前包含产品脚本。
不要在应用中包含商店产品.js代码,因为在此 [ ] 中我们添加了指令,并且没有指令名称"商店产品"
相关文章:
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- 在ES6中,模块将导致多个网络调用,因为两个模块不能在单个文件中定义
- 如何定义一个模块并使用它来分离js文件
- 如何引用HTML中节点模块中的js文件
- 如何使用Node.js在JavaScript模块文件之间使用事件处理程序
- 需要从一个文件中获取多个模块
- 是否可以从javascript文件中将量角器作为模块运行
- 文件结构:使用索引.js文件级联访问子模块或子文件
- ESDoc将我所有的模块方法放在一个标题下,而不是放在每个文件下,我该如何改变这种行为
- Nodejs模块文件和concat
- 如何在RT上添加插件和使用一些外部模块/文件
- jQuery($)在模块文件中不可用
- 将信息从 Drupal 7 中的模块文件传递给 JS 文件
- requireJS和AMD模块/文件夹组织
- 如何将drupal 6中的库添加到模块文件中(运行由flexslider创建的幻灯片)
- 可以webpack为浏览器和节点输出单独的脚本和模块文件
- 我应该把包依赖传递给模块函数,还是只在模块文件中需要它们?
- 将AngularJS应用中的模块文件包含到Karma的RequireJS配置文件中
- 如何从Drupal模块文件中的.js文件调用javascript函数
- 您可以在没有回调的情况下从模块文件中调用主node.js服务器文件中的函数吗