我正在使用grunt连接.js我的Angular应用程序的文件.
我刚刚完成并整理了代码库以遵循此处讨论的约定,特别是将我的代码分组为代表功能的小模块.
但是,如果模块在声明之前被消耗,我发现连接顺序似乎会破坏应用程序.
例如:
|-- src/
| |-- app/
| | |-- userProfile/
| | | | userProfile.js
| | | |-- deposits/
| | | | |-- depositFormCtrl.js
Run Code Online (Sandbox Code Playgroud)
哪里:
// userProfile.js
var userProfile = angular.module('userProfile',[])
// depositFormCtrl.js
angular.module('userProfile')
.controller('DepositFormCtrl', function($scope) {...});
Run Code Online (Sandbox Code Playgroud)
当grunt执行连接时,depositFormCtrl.js出现之前userProfile.js.这导致应用程序抛出错误,抱怨:
未捕获的错误:没有模块:userProfile
我看到很多关于使用RequireJS/AMD来管理模块加载顺序的可能性的讨论.但是,通常会说这是矫枉过正/不需要,因为Angular会为您处理此问题.
例如:Angular团队的Brian Ford 提到:
我个人认为RequireJS做得太多了; AngularJS的DI系统真正缺少的唯一功能是异步加载.
他还在其他地方声明他不建议使用带有Angular的RequireJS.
我也看到提到使用angular-loader.js,如种子项目所示.但是,据我所知,(这里没有官方文档)加载器旨在解决无序加载模块的问题,而不是在使用之前引用它们.
将angular-loader.js添加到我的项目并没有解决问题.
是否有我应该使用的声明来防止我遇到的错误?
声明模块和控制器的正确方法是什么,以便连接顺序文件不会影响运行时的代码?
ed.*_*ed. 16
我有时使用的一种技术是在连接文件的开头放置声明.我这样做是通过将它们放在一个专用文件中,该文件将是串联实用程序拾取的第一个文件.
//app/_declarations.js
angular.module('userProfile',[]);
//app/userProfile/userProfile.js
angular.module('userProfile')
.config(['$routeProvider', function ($router) {...});
//app/userProfile/deposits/depositFormCtrl.js
angular.module('userProfile')
.controller('DepositFormCtrl', function($scope) {...});
Run Code Online (Sandbox Code Playgroud)
可能不适合所有场景,但设置和理解起来很简单.
| 归档时间: |
|
| 查看次数: |
7734 次 |
| 最近记录: |