【发布时间】:2017-02-27 07:04:50
【问题描述】:
我对 angularjs 还是很陌生。我正在使用 browserify 开发应用程序。
当我将require 与字符串一起使用时,它正在工作。但是当我将它与一些组合的字符串一起使用时,它不起作用。
我的意思是 require('./todo') 正在工作,但 var todo = 'todo'; require('./' + todo) 没有。
问题代码index.js在这里。
var fs = require('fs');
var files = fs.readdirSync(__dirname);
var controllers = module.exports = {};
// files.length is 2 and the names are index.js and todo.js
for (var i=0; i<files.length; i++) {
var file = files[i];
if (file !== 'index.js') {
var fileName = file.split('.')[0];
var modulePath = './' + fileName;
// below code is working.
controllers[fileName] = require('./todo');
// below code is not working. The error code -> Uncaught Error: Cannot find module './todo'
controllers[fileName] = require(modulePath);
}
}
有什么问题??
【问题讨论】:
标签: angularjs browserify