如何分离文件中的角度模块和需要使用Browserify
How to separate angular modules in files and require with Browserify?
我使用Angular
进行前端使用,并尝试按模块分离我的应用程序。
对于组织,我希望每个文件单独一个模块,所有控制器都是一个文件到,文件结构示例:
- module1 (paste)
- controllers (paste)
- controller1.js
- controller2.js
- module.js
其中module1.js
需要controller1.js
和controller2.js
,主javascript文件需要所有模块文件。
对于控制器,我是module.exports
控制器的唯一内容,类似于:
// controller1.js
module.exports = ["$scope", function(scope) {
// some code here
}];
在我的模块文件中,我这样要求它:
//module1.js
var app = angular.module("module1", []);
app.service("service1", function() {});
app.controller("controller1", require("./controllers/controller1.js");
module.exports = app;
到目前为止,一切都好。
但当我尝试要求module1.js
到module2.js
使用service1
时,这是未定义service1
的返回。
//module2.js
require('../module1/module.js');
var app = angular.module("module2", ['module1']);
app.controller('controller2-1', ["$scope", "service1", function(scope, service1) {
// some code
});
按文件分离模块并要求使用Browserify
的正确方法是什么?
试试这个:
//child module
var angular = require('angular');
var moduleName = 'moduleChild';
var moduleDependencies = [...];
app.module(moduleName, moduleDependencies)
.controller('foobarctrl', [function(){}]);
module.exports = moduleName;
//parent module
var angular = require('angular');
var moduleChild = require('/path/to/file');
var moduleName = 'moduleChild';
var moduleDependencies = [moduleChild];
app.module(moduleName, moduleDependencies)
.controller('foobarbazctrl', [function(){}]);
module.exports = moduleName;
这种方式是将angular与browserfy一起使用的最干净的方式之一,导出module1对象并不有用,因为你不想重用它,你只需要它的名称。
相关文章:
- 如何在Gulp中使用Browserify lib对模块进行别名
- browserify/requirejs模块和ES6模块有什么区别
- 需要没有 Browserify、Webpack 或 Babel 的 reactjs 模块
- Browserify - 在 package.json 中转换在符号链接模块中不起作用在 npm3 中
- Gulp with browserify:找不到模块 src/js/main.js
- 推荐的组织共享模块 browserify/nodejs 的方法
- 使用Browserify加载海盗湾npm模块客户端
- 在多个Browserify或Webpack捆绑包中共享通过NPM加载的模块的简单解决方案
- 未找到递归要求的Browserify模块(后Babel transfile)
- 如何分离文件中的角度模块和需要使用Browserify
- 从Browserify模块访问DOM时出现问题
- Browserify+Grunt+Remapify:找不到模块
- Browserify-require()一个组合字符串不会将模块构建到输出脚本中
- Browserify+Backbone.js“ApplicationState”共享模块
- Browserify需要使用Browserify构建的供应商模块
- Browserify -如何使变量对我的模块可访问
- Browserify需要-g模块
- 用Browserify可变地加载AngularJS模块
- Browserify独立(UMD)模块的测试覆盖率
- 手动调用模块与使用browserify动态调用模块有何不同?