Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - $resource is not defined

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!

like image 788
Kuba Orlik Avatar asked Jul 03 '13 09:07

Kuba Orlik


2 Answers

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();
});
like image 79
Derek Ekins Avatar answered Sep 27 '22 22:09

Derek Ekins


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.

like image 25
bjm88 Avatar answered Sep 27 '22 22:09

bjm88