[英]AngularJS: Uncaught Error: [$injector:nomod] Module _ is not available! | Multiple References to Module
I am currently trying to reference a single AngularJS module within various directives and services like the one below.我目前正在尝试在各种指令和服务中引用单个 AngularJS 模块,如下所示。
module.js模块.js
(function () {
'use strict';
angular.module('operations.setup.holidays.calendar', []);
})();
When I try to reference it in one directive/service/controller it works fine, but when I try to reference it in say a directive and a service I get: Uncaught Error: [$injector:nomod] Module 'operations.setup.holidays.calendar' is not available!
当我尝试在一个指令/服务/控制器中引用它时它工作正常,但是当我尝试在一个指令和一个服务中引用它时,我得到:
Uncaught Error: [$injector:nomod] Module 'operations.setup.holidays.calendar' is not available!
directive.js (works if this is the only thing referencing 'operations.setup.holidays.calendar'
) directive.js (如果这是唯一引用
'operations.setup.holidays.calendar'
)
(function () {
'use strict';
angular
.module('operations.setup.holidays.calendar')
.directive('yearlyCalendarDirective', yearlyCalendarDirective);
function yearlyCalendarDirective(){
return{
template: "<h1>Year Calendar Directive</h1>"
};
}
})();
service.js (adding something like this causes the specified error) service.js (添加这样的东西会导致指定的错误)
(function(){
'use strict';
angular
.module('operations.setup.holiday.calendar')
.service('Calendar',Calendar);
function Calendar(){
}
})();
Adding something like
.module('operations.setup.holiday.calendar',[])
gets rid of the error, but from what I understand this creates a new module instead of referencing the old one?添加类似
.module('operations.setup.holiday.calendar',[])
可以消除错误,但据我所知,这会创建一个新模块而不是引用旧模块?
According to this answer , calling anonymous functions doesn't guarantee the functions will be calling in the order.根据这个答案,调用匿名函数并不能保证函数会按顺序调用。
maybe you can load all code in one anonymous function:也许您可以在一个匿名函数中加载所有代码:
(function() {
'use strict';
angular.module('CalendarApp', []);
angular.module('CalendarApp').directive('yearlyCalendar', function() {
return {
restrict: 'E',
scope: {},
template: '<span>Here is a YEARLY calendar</span>'
}
});
angular.module('CalendarApp').directive('monthlyCalendar', function() {
return {
restrict: 'E',
scope: {},
template: '<span>Here is a MONTHLY calendar</span>'
}
});
angular.module('CalendarApp').service('CalendarData', function() {
function CalendarData() {
vm = this;
vm.today = new Date();
return new CalendarData();
}
});
})();
if you have this code separated in many files, don't use anonymous functions (calling the code directly instead)如果您将此代码分隔在许多文件中,请不要使用匿名函数(而是直接调用代码)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.