当使用angular 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: '/'
        });
    }
]);

问题出在注入器配置部分(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) 

如果我没记错的话,这个问题是从angular 1.1.6开始的。


当前回答

对于那些使用压缩、捆绑和缩小文件的框架的人,请确保显式地定义每个依赖项,因为这些框架倾向于重命名变量。我在使用ASP时遇到了这种情况。NET BundleConfig.cs将我的应用程序脚本捆绑在一起。

之前

app.config(function($routeProvider) {
    $routeProvider.
      when('/', {
          templateUrl: 'list.html',
          controller: 'ListController'
      }).
      when('/items/:itemId', {
          templateUrl: 'details.html',
          controller: 'DetailsController'
      }).
      otherwise({
          redirectTo: '/'
      });
});

app.config(["$routeProvider", function($routeProvider) {
    $routeProvider.
      when('/', {
          templateUrl: 'list.html',
          controller: 'ListController'
      }).
      when('/items/:itemId', {
          templateUrl: 'details.html',
          controller: 'DetailsController'
      }).
      otherwise({
          redirectTo: '/'
      });
}]);

点击这里阅读更多关于Angular依赖注释的内容。

其他回答

几个月后,我回来开发了一个AngularJS(1.6.4)应用程序,在开发过程中我选择了Chrome (PC)和Safari (MAC)进行测试。此代码在IE 11.0.9600 (Windows 7, 32位)上显示了此Error: $injector:modulerr模块错误。

经过调查,很明显,错误是由于forEach循环被使用,只是将所有forEach循环替换为正常的for循环,以使事情按原样工作…

这基本上是一个IE11问题(在这里回答),而不是AngularJS的问题,但我想把这个回答放在这里,因为引发的异常是一个AngularJS异常。希望这能对我们中的一些人有所帮助。

类似的。不要使用lambda函数…只需replace()=>{…}具有良好的ol' function(){…}

在最后加上这个“()”,我的错误就消失了

(function(){
    var home = angular.module('home',[]);

    home.controller('QuestionsController',function(){
        console.log("controller initialized");
        this.addPoll = function(){
            console.log("inside function");
        };
    });
})();

这个错误的另一个触发点是在你的路由定义中把"."放在" else "或任何其他路由之前:

  app.config(['$routeProvider',
     function($routeProvider) {
        $routeProvider.
           when('/view1', {
              templateUrl: 'partials/view1.html',
              controller: 'Ctrl1'
           }).
           otherwise({
              redirectTo: '/viewCounts'
           });
     }]);

再一次被句号羞辱。爱死JS了!

还有一件事要添加到列表中,因为这是谷歌搜索'Error: [$injector:modulerr] angular'得到的第一个结果:

如果你在你的'index'html'和你的主javascript应用程序定义中有一个不匹配的应用程序名称,这也会产生这个错误。

例如,如果你的HTML是这样的:

    </head>
    <body ng-app="myWebSite">

        <!-- My Web Site -->
        <p>About my web site...</p>

        etc ...

和你的JavaScript看起来像这样(即有一个错别字的应用程序名称- myWebCite而不是myWebSite):

/** Main AngularJS Web Application */ 
var app = angular.module('myWebCite', [ 'ngRoute' ]); 

/** Configure the Routes */ 
app.config(['$routeProvider', function ($routeProvider) { 
  etc ...

那么也会生成'Error:[$injector:modulerr] angular'错误。

对于那些使用压缩、捆绑和缩小文件的框架的人,请确保显式地定义每个依赖项,因为这些框架倾向于重命名变量。我在使用ASP时遇到了这种情况。NET BundleConfig.cs将我的应用程序脚本捆绑在一起。

之前

app.config(function($routeProvider) {
    $routeProvider.
      when('/', {
          templateUrl: 'list.html',
          controller: 'ListController'
      }).
      when('/items/:itemId', {
          templateUrl: 'details.html',
          controller: 'DetailsController'
      }).
      otherwise({
          redirectTo: '/'
      });
});

app.config(["$routeProvider", function($routeProvider) {
    $routeProvider.
      when('/', {
          templateUrl: 'list.html',
          controller: 'ListController'
      }).
      when('/items/:itemId', {
          templateUrl: 'details.html',
          controller: 'DetailsController'
      }).
      otherwise({
          redirectTo: '/'
      });
}]);

点击这里阅读更多关于Angular依赖注释的内容。