最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS - $resource is not defined - Stack Overflow

programmeradmin2浏览0评论

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.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:

Uncaught ReferenceError: $resource is not defined

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!

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.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:

Uncaught ReferenceError: $resource is not defined

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!

Share Improve this question asked Jul 3, 2013 at 9:32 Kuba OrlikKuba Orlik 3,5006 gold badges35 silver badges49 bronze badges 5
  • Could you point to the part of the documentation from where you got this? – Wottensprels Commented Jul 3, 2013 at 9:34
  • @Sprottenwels docs.angularjs.org/api/ngResource.$resource – A. Wolff Commented Jul 3, 2013 at 9:35
  • docs.angularjs.org/api/ngResource.$resource here, quick search on google. app must be your app name, btw. – Samuele Mattiuzzo Commented Jul 3, 2013 at 9:35
  • please provide a jsfiddle, at least all your sources ... html and js – tschiela Commented Jul 3, 2013 at 9:43
  • 3 You are only including the resource module in your app module. But you don't have a controller or service as far as I can see. You need to have one of those, and inject resource in them. – Narretz Commented Jul 3, 2013 at 9:45
Add a comment  | 

2 Answers 2

Reset to default 14

As suggested in the comments you need to create a controller or service that uses the $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();
});

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

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 $resource and $http I guess. Once I removed the $http, which was in there because of me using that lower level api earlier, it started working. Very weird.

发布评论

评论列表(0)

  1. 暂无评论