本文翻译自:AngularJS 1.2 $injector:modulerr
When using angular 1.2 instead of 1.07 the following piece of code is not valid anymore, why? 当使用角度1.2而不是1.07时,下面的代码不再有效,为什么?
'use strict';
var app = angular.module('myapp', []);
app.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.
when('/', {
templateUrl: 'part.html',
controller: 'MyCtrl'
}).
otherwise({
redirectTo: '/'
});
}
]);
the issue is in the injector configuration part (app.config): 问题出在注入器配置部分(app.config):
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.2.0rc1/$injector/modulerr?p0=muninn&p1=Error%…eapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.0rc1%2Fangular.min.js%3A31%3A252)
If I remember correctly this issue started with angular 1.1.6. 如果我没记错的话,这个问题始于angular 1.1.6。
#1楼
参考:https://stackoom.com/question/1EjPO/AngularJS-注入器-modulerr
#2楼
The problem was caused by missing inclusion of ngRoute module. 该问题是由于缺少ngRoute模块的包含而引起的。 Since version 1.1.6 it's a separate part: 从版本1.1.6开始,它是一个单独的部分:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>
var app = angular.module('myapp', ['ngRoute']);
#3楼
Besides below answer, if you have this error in console ( [$injector:nomod]
, MINERR_ASSET:22
), but everything seems to work fine, make sure that you don't have duplicate includes in your index.html. 除了下面的回答,如果你在控制台( [$injector:nomod]
, MINERR_ASSET:22
)中有这个错误,但一切似乎都运行正常,请确保你的index.html中没有重复的包含。
Because this error can also be raised if you have duplicate includes of the files, that use this module, and are included before the file with actual module declaration. 因为如果您有使用此模块的文件的重复包含,并且包含在具有实际模块声明的文件之前,也会引发此错误。
#4楼
If you have this error in console ([$injector:nomod], MINERR_ASSET:22)
, make sure you are not including your application code before loading AngularJS
如果您在控制台([$injector:nomod], MINERR_ASSET:22)
出现此错误,请确保在加载AngularJS
之前未包含应用程序代码
I was doing that and once I fixed the order, the error went away. 我这样做,一旦我修改了订单,错误就消失了。
#5楼
One more thing to add to the list as this is the first result that comes up with a google search of 'Error: [$injector:modulerr] angular': 还有一件事要添加到列表中,因为这是谷歌搜索“错误:[$ injector:modulerr] angular”的第一个结果:
If you have a mismatch between your app name in your 'index'html' and in your main javascript app definition this can also generate this error. 如果您的'index'html'中的应用名称与主javascript应用定义中的应用名称不匹配,则也会产生此错误。
For example if your HTML looks like this: 例如,如果您的HTML看起来像这样:
</head>
<body ng-app="myWebSite">
<!-- My Web Site -->
<p>About my web site...</p>
etc ...
And your JavaScript looks like this (ie has a typo on app name - myWebCite instead of myWebSite): 你的JavaScript看起来像这样(即应用程序名称上有拼写错误 - myWebCite而不是myWebSite):
/** Main AngularJS Web Application */
var app = angular.module('myWebCite', [ 'ngRoute' ]);
/** Configure the Routes */
app.config(['$routeProvider', function ($routeProvider) {
etc ...
then the 'Error:[$injector:modulerr] angular' error will be generated also. 然后还会生成'错误:[$ injector:modulerr] angular'错误。
#6楼
my error disappeared by adding this '()' at the end 我的错误在最后添加'()'消失了
(function(){
var home = angular.module('home',[]);
home.controller('QuestionsController',function(){
console.log("controller initialized");
this.addPoll = function(){
console.log("inside function");
};
});
})();