5
这工作为什么在使用browserify的循环中无法使用(...)?
require('./AppCtrl');
,但如果我这样做
[ './AppCtrl' ].forEach(function(name) {
require(name);
});
它会导致错误
_prelude.js:1 Uncaught Error: Cannot find module './AppCtrl's @ _prelude.js:1s @ _prelude.js:1(anonymous function) @ _prelude.js:1(anonymous function) @ index.js:48 @ index.js:3s @ _prelude.js:1(anonymous function) @ _prelude.js:11../config @ app.js:22s @ _prelude.js:1e @ _prelude.js:1(anonymous function) @ _prelude.js:1
angular.js:12416 Error: [ng:areq] Argument 'AppCtrl' is not a function, got undefined
http://errors.angularjs.org/1.4.5/ng/areq?p0=AppCtrl&p1=not%20a%20function%2C%20got%20undefined
at REGEX_STRING_REGEXP (angular.js:68)
at assertArg (angular.js:1795)
at assertArgFn (angular.js:1805)
at angular.js:9069
at setupControllers (angular.js:8133)
at nodeLinkFn (angular.js:8173)
at compositeLinkFn (angular.js:7637)
at publicLinkFn (angular.js:7512)
at angular.js:1660
at Scope.parent.$get.Scope.$eval (angular.js:15878)
我的最终目标,我想能够做这样的事伪
foreach name {
angular.module(...).controller(require(name));
}
但现在我不确定为什么我不能在循环内部需要文件?
究竟为什么你想这样做吗? – Bergi
,因为我认为手动需要每一个模块是很荒谬的,还有很多重复的代码。 – xenoterracide
要求所有地方都是代码味道,当然也许你的代码有问题?但是,您应该将在一个大型模块中随处可见的那些模块组合在一起,并且只能在任意位置导入。不使用数组和循环。 – Bergi