ChatGPT解决这个技术问题 Extra ChatGPT

AngularJS 1.2 $injector:modulerr

当使用 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 开始的。


s
shoen

该问题是由于缺少包含 ngRoute 模块引起的。从 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']);

如果您不确定缺少哪个模块,请使用未缩小的 angular.js,它会提供可读的错误消息:"Error: [$injector:nomod] Module 'ngRoute' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument."
嗯,我正在使用 bower,它提供了 .map 文件 - 我想知道为什么 Chrome 仍然向我显示缩小文件中的错误。感谢您的提醒,@Mart!
@arg20,你会在这里找到答案:github.com/angular/angular.js/commit/…
天哪,我一直在到处寻找解决方案。我从 1.0 升级到 1.2 并收到此错误。我能找到的唯一建议是确保 angular-route.js 被包括在内,它是……不是 Angular 文档的粉丝。无论如何谢谢你!我投了赞成票。
如果在定义模块时使用 angular.module('myModule') 而不是 angular.module('myModule',[]),则可能会出现此错误。 stackoverflow.com/questions/16260538/…
a
arp

通过在末尾添加此“()”,我的错误消失了

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

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

M
Mick

还有一件事要添加到列表中,因为这是谷歌搜索“错误:[$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 ...

然后也会产生“错误:[$injector:modulerr] angular”错误。


我非常接近这一点。我已经设置了 ng-app="app"... 然后再没有设置模块。所以它触发了错误。作为初学者,我刚刚删除了 ="app" 部分以使我的示例代码正常工作。后来,我设置了正确的模块和路由...... :)
A
Abhishek Gurjar

一个菜鸟错误可能会忘记包含模块 js

<script src="app/modules/myModule.js"></script>

index.html 中的文件


令人惊讶的是,调试/错误消息的角度有多糟糕。
哈哈。我对自己说,不,我不能犯这个简单的错误。我多次通过了你的答案。但它解决了我的问题。
这是关于缺少一些进口
J
Jasdeep Singh

如果您在控制台 ([$injector:nomod], MINERR_ASSET:22) 中遇到此错误,请确保在加载 AngularJS 之前未包含您的应用程序代码

我正在这样做,一旦我修复了订单,错误就消失了。


M
Moses Machua

对于那些使用压缩、捆绑和缩小文件的框架的人,请确保明确定义每个依赖项,因为这些框架倾向于重命名您的变量。这发生在我使用 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: '/'
      });
}]);

Read more here 关于 Angular 依赖注释。


A
Anth12

我刚刚遇到了同样的错误,在我的情况下,它是由 angular.module 中的第二个参数丢失引起的 - 希望这可以帮助遇到同样问题的人。

angular.module('MyApp');

angular.module('MyApp', []);


这是我的问题;我错过了 angular.module('my.Namespace', []); 的声明在使用 angular.module('my.Namespace').directive('myDirective', [...
S
SirajuddinLuck
add to link
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular-route.min.js"></script>

var app = angular.module('apps', [ 'ngRoute' ]); 

忘记了在使用特定依赖项时必须包含 src !
A
Abhijeet Kasurde

我遇到了同样的问题并尝试了所有可能的解决方案。但最后我从文档中得知 ngRoute 模块现在已分离。看看这个link

解决方案:在 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>

H
Hilary

此错误的另一个触发因素是离开“。”在您的“否则”或您的路线定义中的任何其他路线之前:

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

又一次被句号羞辱。一定要爱JS!


p
pleerock

除了以下答案之外,如果您在控制台([$injector:nomod]MINERR_ASSET:22)中遇到此错误,但一切似乎都正常,请确保您的 index.html 中没有重复包含.

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


z
zahid9i

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

注意:从 AngularJS 版本 1.2 开始,ngRoute 在它自己的模块中,必须通过加载额外的 angular-route.js 文件来加载,我们通过上面的 Bower 下载该文件。

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

安装 首先在您的 HTML 中包含 angular-route.js:您可以从以下位置下载此文件:Google CDN 例如 //ajax.googleapis.com/ajax/libs/angularjs/XYZ/angular-route.js Bower 例如 bower install angular-route@XYZ code.angularjs.org 例如 "//code.angularjs.org/XYZ/angular-route.js" 其中 XYZ 是您正在运行的 AngularJS 版本。然后通过将模块添加为依赖模块将其加载到您的应用程序中: angular.module('app', ['ngRoute']);有了它,你就可以开始了!


J
Josh

John Papa 在this相当晦涩的评论中提供了我的问题:有时当您收到该错误时,这意味着您丢失了一个文件。 其他时候表示模块是在使用后定义的。轻松解决此问题的一种方法是将模块文件命名为 *.module.js 并首先加载它们。


J
J-Alex

它是一个喷油器错误。你可能使用了很多 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 的注射器


F
Frank Conry

我的问题出在 config.xml 中。改变:

<access origin="*" launch-external="yes"/>

<access origin="*"/>

解决它。


S
Shashi Ranjan

有时,当您在项目之间切换并在同一端口上一个接一个地运行项目时,我会看到此错误。在这种情况下,转到 chrome 开发人员工具 >网络并尝试检查实际写入的 js 文件:if the file match with the workspce。要解决此问题,请选择网络下的 Disable Cache


A
Akber Iqbal

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

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

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

相似地。不要使用 lambda 函数...只需将 ()=>{...} 替换为好的 ol' function(){...}


A
Aref Bozorgmehr

我遇到了这个问题,在逐行检查我的代码后,我看到了这个

 <textarea class="form-control" type="text" ng-model="WallDesc" placeholder="Enter Your Description"/>

我只是把它改成

     <textarea class="form-control" type="text" ng-model="WallDesc" placeholder="Enter Your Description"></textarea>

它奏效了。所以检查你的标签。


关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅