我的应用程序中声明了一堆Angular模块。我最初开始使用“链式”语法声明它们,如下所示:
angular.module('mymodule', [])
.controller('myctrl', ['dep1', function(dep1){ ... }])
.service('myservice', ['dep2', function(dep2){ ... }])
... // more here
但是我认为这不太容易阅读,所以我开始使用如下模块变量来声明它们:
var mod = angular.module('mymodule', []);
mod.controller('myctrl', ['dep1', function(dep1){ ... }]);
mod.service('myservice', ['dep2', function(dep2){ ... }]);
...
第二种语法对我来说似乎更具可读性,但我唯一的抱怨是该语法将mod
变量保留在全局范围之外。如果我有其他名为的变量mod
,那么下一个变量(以及与全局变量相关的其他问题)将覆盖它。
所以我的问题是,这是最好的方法吗?还是做这样的事会更好?
(function(){
var mod = angular.module('mymod', []);
mod.controller('myctrl', ['dep1', function(dep1){ ... }]);
mod.service('myservice', ['dep2', function(dep2){ ... }]);
...
})();
还是足够重要呢?只是想知道模块声明的“最佳实践”是什么。提前致谢。
声明模块的“最佳”方法
由于angular本身在全局范围内,并且模块已保存到其变量中,因此您可以通过angular.module('mymod')
以下方式访问模块:
// one file
// NOTE: the immediately invoked function expression
// is used to exemplify different files and is not required
(function(){
// declaring the module in one file / anonymous function
// (only pass a second parameter THIS ONE TIME as a redecleration creates bugs
// which are very hard to dedect)
angular.module('mymod', []);
})();
// another file and/or another anonymous function
(function(){
// using the function form of use-strict...
"use strict";
// accessing the module in another.
// this can be done by calling angular.module without the []-brackets
angular.module('mymod')
.controller('myctrl', ['dep1', function(dep1){
//..
}])
// appending another service/controller/filter etc to the same module-call inside the same file
.service('myservice', ['dep2', function(dep2){
//...
}]);
// you can of course use angular.module('mymod') here as well
angular.module('mymod').controller('anothermyctrl', ['dep1', function(dep1){
//..
}])
})();
不需要其他全局变量。
当然,这完全取决于偏好,但是我认为这是最好的做法,因为
用于对模块和文件进行排序的选项
这种声明和访问模块的方式使您非常灵活。您可以通过功能类型(如另一个答案中所述)或通过路由对模块进行排序,例如:
/******** sorting by route **********/
angular.module('home')...
angular.module('another-route')...
angular.module('shared')...
最终如何排序取决于个人喜好以及项目的规模和类型。我个人喜欢将模块的所有文件归入同一文件夹(按指令,控制器,服务和过滤器的子文件夹排序)中的所有文件,包括所有不同的测试文件,因为它使您的模块更具可重用性。因此,在中型项目中,我最终得到一个基本模块,其中包括所有基本路径及其控制器,服务,指令和或多或少复杂的子模块,当我认为它们也可能对其他项目有用时,例如:
/******** modularizing feature-sets **********/
/controllers
/directives
/filters
/services
/my-map-sub-module
/my-map-sub-module/controllers
/my-map-sub-module/services
app.js
...
angular.module('app', [
'app.directives',
'app.filters',
'app.controllers',
'app.services',
'myMapSubModule'
]);
angular.module('myMapSubModule',[
'myMapSubModule.controllers',
'myMapSubModule.services',
// only if they are specific to the module
'myMapSubModule.directives',
'myMapSubModule.filters'
]);
对于非常大的项目,有时我会如上所述按路线或按某些选定的主要路线,甚至是路线和某些选定组件的组合对模块进行分组,但这确实取决于。
编辑:只是因为它是相关的,并且我最近又遇到了这个问题:请注意,您只能创建一个模块一次(通过向angular.module-function添加第二个参数)。这会使您的应用程序混乱,并且很难检测。
2015年对排序模块的编辑:一年半的经验之后,我可以补充一点,因为在AMD中仍然无法很好地与Angular以及服务,指令和过滤器配合使用,因此在您的应用程序中使用命名不同的模块所带来的收益有限无论如何都可以在角度上下文中全局使用(如此处所示)。但是,仍然存在语义和结构上的好处,并且能够包含/排除带有注释掉或注释掉一行代码的模块可能会有所帮助。
由于子模块通常彼此依赖,因此按类型分隔子模块(例如,“ myMapSubModule.controllers”)几乎也没有任何意义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句