当使用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依赖注释的内容。

其他回答

除了下面的答案,如果你在控制台([$injector:nomod], MINERR_ASSET:22)中出现这个错误,但一切似乎都正常工作,请确保你的index.html中没有重复的include。

因为如果您有使用此模块的文件的重复包含,并且包含在具有实际模块声明的文件之前,也会引发此错误。

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

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

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

如果你浏览angularjs的官方教程https://docs.angularjs.org/tutorial/step_07

注意:从AngularJS的1.2版本开始,ngRoute就独立了 模块,必须通过加载额外的angular-route.js来加载 文件,我们通过上面的Bower下载。

另外请注意ngRoute api https://docs.angularjs.org/api/ngRoute

Installation First include angular-route.js in your HTML: You can download this file from the following places: Google CDN e.g. //ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-route.js Bower e.g. bower install angular-route@X.Y.Z code.angularjs.org e.g. "//code.angularjs.org/X.Y.Z/angular-route.js" where X.Y.Z is the AngularJS version you are running. Then load the module in your application by adding it as a dependent module: angular.module('app', ['ngRoute']); With that you're ready to get started!

这是注入器错误。你可能使用了很多JavaScript文件,所以注入器可能会丢失。

这里有一些:

var app = angular.module('app', 
    ['ngSanitize', 'ui.router', 'pascalprecht.translate', 'ngResource', 
     'ngMaterial', 'angularMoment','md.data.table', 'angularFileUpload', 
     'ngMessages', 'ui.utils.masks', 'angular-sortable-view',          
     'mdPickers','ngDraggable','as.sortable', 'ngAnimate', 'ngTouch']
);

请检查你需要在app.js中插入的注入器

我也遇到了同样的问题,尝试了所有可能的解决方法。但最后我从文档中得知,ngRoute模块现在是分开的。看看这个链接

解决方案: 在angular.min.js脚本后添加cdn angular-route.js

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>