Cordova混合应用程序设计-控制器

Cordova Hybrid App Design - Controllers

本文关键字:控制器 程序设计 应用 混合 Cordova      更新时间:2023-09-26

我正在尝试使用Cordova框架开发一个应用程序。我的应用程序需要有许多控制器才能工作。因此,随着代码大小的增加,将所有控制器保持在同一文件中将是庞大的。因此,代码的可维护性将丧失。

因此,对于每个控制器,建议采用哪种编程设计方法:

1.有一个主App.js文件,并在其中写入所有控制器。最后在index.html文件中只导入App.js文件

var myApp = angular.module('myApp', []);
myApp.controller('ControllerA', ['$scope', function($scope) {
}]);
myApp.controller('ControllerB', ['$scope', function($scope) {
}]);
myApp.controller('ControllerC', ['$scope', function($scope) {
}]);

2.每个控制器有多个js文件,并在主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) {
}]);

当然,出于版本控制的目的,将这些控制器分离到自己的文件中是理想的。我认为您的项目可以从Grunt或GulpJS这样的任务运行程序中受益。

如果你使用GulpJS,你可以这样设置你的gullfile:

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,其中包括你的应用程序和任何数量的控制器、服务和指令。