我有一个大型的 angularjs 应用程序。它采用模块模式构建,如下所示:
./
/search
search.js
/admin
/users
users.js
/settings
settings.js
admin.js
/edit
edit.js
/common
/components
/modal
modal.js
/services
auth.js
等等。目前它们都在一个 angularjs“模块”下,例如:
var app = var app = angular.module('myapp', []);
然后在后续的 Controller /等中我这样做
app.controller('SearchCtrl', function(){ ... })
但是当我在网上浏览诸如 Angular CRUD Demo 之类的示例时和 ngBolierplate我发现他们中的大多数人将每个嵌套模块分成自己的“模块”,而不是仅仅将其绑在主模块上。
这里的最佳实践是什么?我已经看到了两者,但不确定哪一个是“Angular 方式”。
最佳答案
取自 here ,链接到 official angular blog并称为最佳实践(强调我的):
Module definitions
In general, 'angular.module('foo')' should be called only once. Other modules and files can depend on it, but they should never modify it. Module definition can happen in the main module file, or in subdirectories for sections or components, depending on the application's needs.
所以,我认为首选方法是让每个 .js
文件声明自己的模块,然后在根目录的主 app.js
文件中引用这些模块项目的情况。
关于AngularJS 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22519669/