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