我正在使用grunt连接我的Angular应用程序的.js
文件。AngularJS:文件连接中断模块声明顺序
我刚刚完成并整理了代码库,以遵循here讨论的约定,特别是将我的代码分组为代表功能的小模块。
但是,我发现串联的顺序似乎破坏了应用程序,如果模块在声明之前被使用。
如:
|-- src/
| |-- app/
| | |-- userProfile/
| | | | userProfile.js
| | | |-- deposits/
| | | | |-- depositFormCtrl.js
其中:
// userProfile.js
var userProfile = angular.module('userProfile',[])
// depositFormCtrl.js
angular.module('userProfile')
.controller('DepositFormCtrl', function($scope) {...});
当咕噜进行级联,depositFormCtrl.js
userProfile.js
出现之前。这将导致应用程序引发错误,抱怨:
Uncaught Error: No module: userProfile
我看到很多谈论使用RequireJS的可能性/ AMD管理模块的加载顺序。但是,经常说这是过度/不需要的,as Angular handles this for you。
E.g:对角队mentioned的布赖恩·福特:
My personal take is that RequireJS does too much; the only feature that AngularJS's DI system is really missing is the async loading.
他还指出elsewhere,他不与角推荐RequireJS。
我也见过提到使用angular-loader.js,如seed project所示。然而,据我所知,装载程序旨在解决加载模块失序的问题,而不是在使用前引用它们。
将angular-loader.js添加到我的项目中并未解决问题。
是否有我应该使用的声明来防止我遇到的错误?
什么是正确的方式来声明模块&控制器,以便命令文件连接不会影响运行时的代码?
这就是为什么我建议使用RequireJS有棱有角。 RequireJS让您可以轻松定义所有脚本的加载顺序,而且,在部署到生产环境时,您可以使用r.js(RequireJS优化器),它可以跟随加载顺序定义文件,并按照以下顺序准确连接文件你指示它这样做。这就是我在我的项目中所做的,它完美的工作。 – Stewie
我不知道这是否能解决您的问题(它是我的)。但我不得不加载app.js第一(该文件与'VAR app'声明,然后我可以通配符文件的其余部分与'咕噜-的contrib-concat' – chovy