【问题标题】:AngularJS Error :Module 'ngResource' is not available! You either misspelled the module name or forgot to load itAngularJS 错误:模块“ngResource”不可用!您要么拼错了模块名称,要么忘记加载它
【发布时间】:2014-12-06 23:17:53
【问题描述】:

我正在尝试从 Angular js 进行服务调用。我下载了angular seed project。并在view1.js里面写了如下代码来调用服务:

'use strict';

angular.module('myApp.view1', ['ngRoute','ngResource'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/view1', {
    templateUrl: 'view1/view1.html',
    controller: 'View1Ctrl'
  });
}])
.factory('Entry', ['$resource',function($resource) {
  return $resource('http://localhost:8000/emp/'); // Note the full endpoint address
}])
.controller('View1Ctrl', ['Entry',function(Entry) {
  var entries = Entry.query(function() {
    console.log(entries);
  });
}]);

这是index.html,我在其中包含了所需的脚本:

    <!DOCTYPE html>
<!--[if lt IE 7]>      <html lang="en" ng-app="myApp" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html lang="en" ng-app="myApp" class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html lang="en" ng-app="myApp" class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en" ng-app="myApp" class="no-js"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>My AngularJS App</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="bower_components/html5-boilerplate/css/normalize.css">
  <link rel="stylesheet" href="bower_components/html5-boilerplate/css/main.css">
  <link rel="stylesheet" href="app.css">
  <script src="bower_components/html5-boilerplate/js/vendor/modernizr-2.6.2.min.js"></script>
</head>
<body>
  <ul class="menu">
    <li><a href="#/view1">view1</a></li>
    <li><a href="#/view2">view2</a></li>
  </ul>

  <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
  <![endif]-->

  <div ng-view></div>

  <div>Angular seed app: v<span app-version></span></div>

  <!-- In production use:
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/x.x.x/angular.min.js"></script>
  -->
  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/angular-route/angular-route.js"></script>
  <script src="angular-resource.js"></script>
  <script src="app.js"></script>
  <script src="view1/view1.js"></script>
  <script src="view2/view2.js"></script>
  <script src="components/version/version.js"></script>
  <script src="components/version/version-directive.js"></script>
  <script src="components/version/interpolate-filter.js"></script>
</body>
</html>

但在运行应用程序时,它在浏览器控制台中显示以下错误:

    Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module myApp.view1 due to:
Error: [$injector:modulerr] Failed to instantiate module ngResource due to:
Error: [$injector:nomod] Module 'ngResource' 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.

【问题讨论】:

  • 试试['Entry',function(Entry) {
  • 好吧,这似乎引发了另一个错误Error: [$injector:unpr] Unknown provider: $resourceProvider &lt;- $resource &lt;- Entry
  • ngResource 添加到angular.module('myApp.view1', ['ngRoute','ngResource'])
  • 能否在问题中包含 HTML 代码。
  • 试试 angular.module('myApp', ['ngRoute','ngResource'])

标签: javascript angularjs ngresource


【解决方案1】:
<script src="angular-resource.js"></script>

应该是:

<script src="bower_components/angular-resource/angular-resource.js"></script>

【讨论】:

    【解决方案2】:

    唯一有意义的是 angular-resource.js 文件没有正确加载,你能看看网络选项卡,看看实际加载了什么文件以及模块的名称是什么?以确保您输入正确。

    (抱歉没有发表评论,还没有足够的代表)。

    【讨论】:

      【解决方案3】:

      您缺少依赖项列表。

      如果你传递一个数组作为依赖注入,你需要告诉 angular。

      ['Entry', function (Entry) { }]; 应该可以解决问题,或者直接使用function (Entry) { }

      Entry 未定义的原因是因为 Angular 不知道你需要注入什么(你将数组符号传递给 Angular 的 DI 系统)

      ps:英语不好请见谅

      【讨论】:

      • @iJay 抱歉回复晚了,我看到&lt;script src="angular-resource.js"&gt;&lt;/script&gt; 是正确的路径吗?因为您的 html 中的大部分 angularjs 路径似乎都在 bower_components 目录中:)
      猜你喜欢
      • 2017-06-29
      • 2017-09-26
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      相关资源
      最近更新 更多