javascript AngularJS - $resource 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17444446/
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-10-27 08:21:11  来源:igfitidea点击:

AngularJS - $resource is not defined

javascriptangularjs

提问by Kuba Orlik

I'm trying to get a grasp of Angular's ngResource. I've started with a simple code excerpt taken from Angular's documentation:

我试图掌握 Angular 的 ngResource。我从摘自 Angular 文档的简单代码开始:

angular.module("app", ['ngResource'])
var user = $resource("/REST/user/:id", {userID:'@id'});

But when the code is run I check the JS console and I see an error saying:

但是当代码运行时,我检查了 JS 控制台,我看到一条错误消息:

Uncaught ReferenceError: $resource is not defined

未捕获的 ReferenceError: $resource 未定义

Yes, I've included the 'angular-resource.js' script. I think I'm omitting something obvious, but I can't deduce what it is. Please help!

是的,我已经包含了“angular-resource.js”脚本。我想我省略了一些明显的东西,但我无法推断出它是什么。请帮忙!

回答by Derek Ekins

As suggested in the comments you need to create a controller or service that uses the $resource.

正如评论中所建议的,您需要创建一个使用 $resource 的控制器或服务。

Here is an example

这是一个例子

var app = angular.module('plunker', ['ngResource']);

app.controller('MainCtrl', function($scope, $resource) {
  var dataService = $resource('http://run.plnkr.co/5NYWROuqUDQOGcKq/test.json');
  $scope.data = dataService.get();
});

回答by bjm88

FYI incase it helps anyone, I had this which wasn't working. The $resourceinitialization was just returning undefined null, no resource service object.

仅供参考,它可以帮助任何人,我有这个不起作用。在$resource初始化刚刚返回undefined空,没有资源的服务对象。

myConstrollers.controller('ConsumerListController',['$scope', '$http','$resource','ConsumerService',function($scope, $http,$resource, ConsumerService) {
    $scope.consumers=ConsumerService.query();
  }]);

There was no error. It turns out for some reason you cannot use $resourceand $httpI guess. Once I removed the $http, which was in there because of me using that lower level api earlier, it started working. Very weird.

没有错误。事实证明由于某种原因你不能使用$resource$http我猜。一旦我删除了$http,因为我之前使用了较低级别的 api 而在那里,它开始工作。很奇怪。