AngularJS 1.2 $注入器:modulerr

当从AngularJS 1.0.7升级到1.2时,可能会遇到'$injector:modulerr'错误。常见原因包括ngRoute模块缺失、重复的文件包含、应用加载顺序错误以及应用程序名称不匹配。确保正确引入ngRoute,避免重复的HTML引用,并按正确顺序加载应用程序代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自: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");
        };
    });
})();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值