AngularJS模块和控制器在不同的文件中

时间:2018-01-15 16:34:33

标签: angularjs gulp coffeescript

我想将控制器移动到它自己的文件中,同时将模块配置文件放在单独的文件中

现在我有了这段代码:

# Module part
voucherModule = angular.module(
    'Backoffice.Vouchers'
    [
        'Core.Repository.Voucher'
        'ui.router'
    ]
)

voucherModule.config ($stateProvider) ->
    $stateProvider
        .state 'vouchers',
        url: '/vouchers'
        data:
            title : 'Vouchers'
        views:
            "main":
                controller: 'VoucherListController'
                templateUrl: 'views/vouchers/voucher-list.html'

# Controller part. Below should go to different file.

class VoucherListController extends BaseController
    @register voucherModule
    @inject '$scope', '$state', '$rootScope', 'VoucherRepository'

我有很多.coffee个文件,使用gulp编译为单个文件。

当我删除控制器代码并将其移动到不同的文件时,我收到以下错误:

  

错误:[$ injector:modulerr]由于以下原因无法实例化模块Backoffice.App:
  [$ injector:modulerr]无法实例化模块Backoffice.Shop,因为:
  [$ injector:nomod]模块' Backoffice.Shop'不可用!您要么错误拼写了模块名称,要么忘记加载它。

您可以看到它甚至没有相关的模块错误。 我看到这次编译有所不同:

--- BEFORE ---
var VoucherListController, voucherModule;

voucherModule = angular.module('Backoffice.Vouchers', ['Core.Repository.Voucher', 'ui.router']);
voucherModule.config(function($stateProvider) {
  return $stateProvider.state('vouchers', {/*..*/});
});

VoucherListController = (/* controller-code */)(BaseController);

--- AFTER ---
var VoucherListController;

VoucherListController = (/* controller-code */)(BaseController);

var voucherModule;
voucherModule = angular.module('Backoffice.Vouchers', ['Core.Repository.Voucher', 'ui.router']);
voucherModule.config(/*..*/);

Gulp文件任务:

gulp.task('build-core', function () {
    return gulp.src(sources.coffee.core) // AngularCore/**/*.coffee
        .pipe(continueOnError(coffee({bare:true})).on('error', gutil.log))
        .pipe(concat('core.js'))
        .pipe(gulp.dest(destinations.javascript.core)) // app/js/lib/
});

有人可以帮我弄清楚出了什么问题吗?基本上模块和控制器代码会切换位置和var位置移动。

1 个答案:

答案 0 :(得分:0)

正如Izagkaretos所说,我只需要在gulp配置中将路径分成多个部分,然后一切正常。

gulp.task('build-core', function () {
    return gulp.src(sources.coffee.core) // [AngularCore/**/*Module.coffee, AngularCore/**/*.coffee]
        .pipe(continueOnError(coffee({bare:true})).on('error', gutil.log))
        .pipe(concat('core.js'))
        .pipe(gulp.dest(destinations.javascript.core)) // app/js/lib/
});
相关问题