【问题标题】:Angular: 'modulerr', "Failed to instantiate moduleAngular:'modulerr',“无法实例化模块
【发布时间】:2015-07-26 16:24:23
【问题描述】:

我正在尝试学习 Angular,但在尝试向我的应用程序添加路由时遇到了困难。

我不断收到此错误

'modulerr', "无法实例化模块

从其他 stackoverflow 问题中,我不是从正确加载 ngRoute 获得的,而是在头部加载了 angular-route.js,并且在我的模块构造中声明了 ngRoute,所以我有点困惑

我的索引文件如下

<!DOCTYPE html>
<html lang="en">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-route.js"></script>
  <script src="https://cdn.firebase.com/js/client/2.2.4/firebase.js"></script>
  <script src="https://cdn.firebase.com/libs/angularfire/1.1.2/angularfire.min.js"></script>
  <script src="js/app.js"></script>
  <script src="js/animations.js"></script>
  <script src="js/controllers.js"></script>
  <script src="js/filters.js"></script>
  <script src="js/services.js"></script>
</head>
<body ng-app="pgpChatApp">
  <div class="view-container">
    <div ng-view="" class="view-frame"></div>
  </div>
</body>
</html>

我的应用文件

'use strict';

var pgpChatApp = angular.module('pgpChatApp', [
  'ngRoute',
  'firebase',
  'pgpChatAnimations',
  'pgpChatControllers',
  'pgpChatFilters',
  'pgpChatServices'
]);

pgpChatApp.config(["$routeProvider", function ($routeProvider) {
  $routeProvider.when("/home", {
    // the rest is the same for ui-router and ngRoute...
    controller: "userAuth",
    templateUrl: "partials/home.html"
  }).when("/account", {
    // the rest is the same for ui-router and ngRoute...
    controller: "AccountCtrl",
    templateUrl: "partials/msg_room.html",
    resolve: {
      // controller will not be loaded until $requireAuth resolves
      // Auth refers to our $firebaseAuth wrapper in the example above
      "currentAuth": ["Auth", function (Auth) {
        // $requireAuth returns a promise so the resolve waits for it to complete
        // If the promise is rejected, it will throw a $stateChangeError (see above)
        return Auth.$requireAuth();
      }]
    }
  });
}]);

我的控制器文件

var pgpChatAppControllers = angular.module('pgpChatAppControllers', []);

pgpChatAppControllers.controller("userAuth", ["$scope", "$routeParams", "Auth",
  function ($scope, $routeParams, Auth) {
    $scope.createUser = function () {
      $scope.message = null;
      $scope.error = null;

      Auth.$createUser({
        email: $scope.email,
        password: $scope.password
      }).then(function (userData) {
        $scope.message = "User created with uid: " + userData.uid;
      }).catch(function (error) {
        $scope.error = error;
      });
    };

    $scope.removeUser = function () {
      $scope.message = null;
      $scope.error = null;

      Auth.$removeUser({
        email: $scope.email,
        password: $scope.password
      }).then(function () {
        $scope.message = "User removed";
      }).catch(function (error) {
        $scope.error = error;
      });
    };
  }]);

有人知道解决方法是什么吗?

在此致谢

[编辑] 完整的异常消息

http://errors.angularjs.org/1.3.15/$injector/modulerr?p0=pgpChatAnimations&p1=错误:[$injector:nomod] http://errors.angularjs.org/1.3.15/$injector/nomod?p0=pgpChatAnimations 在错误(本机) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:6:417https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:21:412 在 (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:21:53) 在 w.bootstrap (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:21:296) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:35:116 在 r (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:7:302) 在 g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:34:399) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:35:63 在 r (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:7:302)"

【问题讨论】:

  • 你的代码在哪里引用了“modulerr”? *注意双 r。
  • 在 angularfire 之后加载 angular 会导致额外的错误,angularfire 是 angular 的 firebase 插件。 @AndrewF 我在上面包含了完整的异常消息
  • 在第一个文件中你有var pgpChatApp = angular.module('pgpChatApp', ...),然后在第二个文件中:var pgpChatAppControllers = angular.module('pgpChatAppControllers', [])——你可能不想要第二个模块,应该将控制器添加到pgpChatApp
  • 好的,那么您应该按原样加载 js 文件,但没有协议。喜欢src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"。确保所有这些
  • @JamesKirkby 创建小提琴 ..

标签: javascript jquery angularjs html model-view-controller


【解决方案1】:

angular-route 的 url 不起作用。尝试使用:

http://code.angularjs.org/1.2.0-rc.3/angular-route.js

【讨论】:

  • 是的,抱歉,我使用的是不同版本的 Angular js。我删除了对动画、过滤器和服务的引用,因为您没有提供这些来源并且应用程序运行良好......这让我认为错误出现在其中一个文件中。
  • 另外,您应该将您的 app.js 作为最后一个文件,因为它可能不会在运行之前等待其他文件加载...这意味着它可能找不到您其他模块的定义。
  • 正确的方法首先加载 app.js,因为您正在定义稍后使用的 pgpChatApp,请参阅此角度演示 github.com/angular/angular-phonecat/blob/master/app/index.html
【解决方案2】:

我想我的模块名称仍然与 app.js 中定义的模块不匹配

这个差异显示了修复

https://github.com/jkirkby91/angular-firebase-chat/commit/35be74592197d16435adb322f0e24963108ed97a

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 2014-06-04
    相关资源
    最近更新 更多