AngularJS 不同文件中的模块和控制器
AngularJS module and controller in different files
我想将我的控制器移动到它自己的文件中,同时将模块配置文件放在单独的文件中
现在我有这个代码:
# 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 编译成单个文件。
当我删除控制器代码并将其简单地移动到不同的文件时,出现以下错误:
Error: [$injector:modulerr] Failed to instantiate module Backoffice.App due to:
[$injector:modulerr] Failed to instantiate module Backoffice.Shop due to:
[$injector:nomod] Module 'Backoffice.Shop' is not available! You either misspelled the module name or forgot to load it.
如您所见,它甚至不是相关的模块错误。
我看到这次编译不一样了:
--- 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
位置移动。
正如 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/
});
我想将我的控制器移动到它自己的文件中,同时将模块配置文件放在单独的文件中
现在我有这个代码:
# 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 编译成单个文件。
当我删除控制器代码并将其简单地移动到不同的文件时,出现以下错误:
Error: [$injector:modulerr] Failed to instantiate module Backoffice.App due to:
[$injector:modulerr] Failed to instantiate module Backoffice.Shop due to:
[$injector:nomod] Module 'Backoffice.Shop' is not available! You either misspelled the module name or forgot to load it.
如您所见,它甚至不是相关的模块错误。 我看到这次编译不一样了:
--- 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
位置移动。
正如 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/
});