我正在尝试使用Cordova框架开发一个Application。我的应用程序需要有许多控制器才能工作。因此,随着代码大小的增加,将所有控制器保留在同一个文件中将是庞大的。因此代码的可维护性将会丢失。
因此,对于每个控制器建议编程设计方法:
App.js
文件并在其中写入所有控制器。最后只导入App.js
文件中的index.html
文件。var myApp = angular.module('myApp', []);
myApp.controller('ControllerA', ['$scope', function($scope) {
}]);
myApp.controller('ControllerB', ['$scope', function($scope) {
}]);
myApp.controller('ControllerC', ['$scope', function($scope) {
}]);
index.html
文件中导入所有文件:主App.js文件:
var myApp = angular.module('myApp', []);
ControllerA.js文件:
myApp.controller('ControllerA', ['$scope', function($scope) {
}]);
ControllerB.js文件:
myApp.controller('ControllerB', ['$scope', function($scope) {
}]);
ControllerC.js文件:
myApp.controller('ControllerC', ['$scope', function($scope) {
}]);
答案 0 :(得分:1)
当然,出于版本控制的目的,将这些控制器分离到自己的文件中是理想的。我认为您的项目可以从Grunt或GulpJS等任务运行器中受益。
如果您使用GulpJS,您可以设置您的gulpfile:
var gulp = require('gulp');
var concat = require('gulp-concat');
var ug = require('gulp-uglify');
var rename = require('gulp-rename');
// Minfy all assets into a single file.
gulp.task('default', function() {
var files = [
'/js/controller/*.js'
];
return gulp.src(files)
.pipe(concat('bundle.js'))
.pipe(ug())
.pipe(gulp.dest('../js'));
});
然后从命令行运行gulp
。在您的页面上,您将加载整个bundle.js,其中包括您的应用程序和任意数量的控制器,服务和指令。