错误: [$injector:modulerr]

13 浏览
0 Comments

错误: [$injector:modulerr]

我是一名新手。正在学习/尝试将Angular与我的Web应用程序(Java + jQuery + requireJS)集成。我没有使用任何路由器,下面是我的脚本。从其他stackoverflow中我了解到,这个错误是由于缺少ngRoute模块引起的。自1.1.6版本以来,它是一个单独的部分。但在我的下面代码中,我根本没有使用任何ngRouter。当我没有引用它时,为什么会出现这个错误呢?

Error: [$injector:modulerr] Failed to instantiate module counter due to: [$injector:nomod] Module \'counter\' 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. http://errors.angularjs.org/1.2.11/$injector/nomod?p0=counter

模板:

 

 

 

 

 

JS

requirejs.config({
  paths : {  angular: "/js/lib/angular/angular.min" },
  shim : {  "angular": {  deps: [ "jquery"],  exports: "angular" } }
});
require(["angular", "jquery"],function() {
var module = angular.module('counter', []);
module.controller('CounterController', function ($scope, $http, $timeout) {
    $scope.critical = 0;
    $scope.error = 0;
    $scope.warn = 0;
    $scope.note = 0;
    function setData(d){
        $scope.critical = d.critical;
        $scope.error = d.error;
        $scope.warn = d.warn;
        $scope.note = d.note;
    }
    var getCounters = function() {
        var config = {headers: {
                'X-MY-Request': (new Date()).getMilliseconds()
            }
        };
        $http.get('xxxxxxx', config)
                .success(function(data, status, headers, config) {
            setData(data);
            $timeout(getCounters, 60000);
        }).error(function(data, status, headers, config) {
            // Handle the error
        });
    }
    $timeout(getCounters, 500);
});

admin 更改状态以发布 2023年5月24日
0
0 Comments

我遇到了同样的问题,为了获得更多有关错误的信息,请使用{debugInfoEnabled:true}调用引导函数,如下所示:

angular.bootstrap(document.getElementById('app-root'), ['MyApp'], {debugInfoEnabled: true});

这将在控制台上打印出无法实例化的基础模块是什么。

0
0 Comments

在你的模板中,你的控制器名称的第一个字母是小写的counterController。你的js中使用了大写的CounterController。这可能是问题所在吗?

任何未找到的注入模块都会抛出错误。不仅仅是ngRoute。

编辑

我刚刚快速地创建了一个plunker,在没有 'require' 的情况下你的代码是正确的。所以你的angular代码没有遗漏任何东西。所有的模块都能够工作。

所以问题必须出在你的 'require' 建立上。我从来没有用过它,所以不能真正地帮助你。在这里查看更多资料:Angular + Requirejs - Loading in the wrong order.

现在,如果我是你,我会逐个学习技术。只要加载你的angular和jquery资源,然后学习如何使用它们。然后当你变得更加熟练时再添加 'require'。

0