Javascript AngularJS 错误:模块“ngResource”不可用!您拼错了模块名称或忘记加载它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26322248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:43:14  来源:igfitidea点击:

AngularJS Error :Module 'ngResource' is not available! You either misspelled the module name or forgot to load it

javascriptangularjsngresource

提问by iJade

I'm trying to make a service call from angular js. I downloaded the angular seed project.And inside the view1.jswrote the following code to call the service:

我正在尝试从 angular js 进行服务调用。我下载了angular 种子项目。在里面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);
??});
}]);

Here is the index.htmlwhere I have included the required scripts:

这是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>

But on running the app, it shows the following error in the browser console:

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

    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.

回答by JStark

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

should be:

应该:

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

回答by PiniH

The only thing that makes sense is that the angular-resource.js file isn't loaded properly, can you have a look at the network tab and see what file is actually loaded and what the name of the module is? to make sure you typed it in correctly.

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

(Sorry for not commenting, not enough rep yet).

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

回答by sendy halim

You're missing dependency list.

您缺少依赖项列表。

If you're passing an array as dependeny to be injected, you need to tell angular.

如果您将数组作为要注入的依赖项传递,则需要告诉 angular。

['Entry', function (Entry) { }];should do the trick, or just do function (Entry) { }.

['Entry', function (Entry) { }];应该做的伎俩,或者只是做function (Entry) { }

the reason why Entryis undefined, is because angular does not know what u need to be injected (you're passing an array notation to the angular's DI system)

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

ps: sry for bad english

ps:sry 英语不好