teepusink
teepusink

Reputation: 28922

Angular - function in service not recognizing $resource

I'm working with Angular factory and trying to make a webservice call, but looks like it is not recognizing $resource.

.factory('User', function($resource) {
function login() {
  // THIS IS NOT WORKING, how do I make a webservice call from here?
  $resource('/api/user.json', {}, {

      method: 'POST',
      isArray: false

  });

  return true;
}

function logout() {
  return false;
}

return {
  login : function() { return login(); },
  logout: function() { return logout(); }
};

Thanks,
Tee

Upvotes: 0

Views: 3436

Answers (1)

mpm
mpm

Reputation: 20155

i assume you have the resource module file somewhere :

https://raw.github.com/angular/angular.js/master/src/ngResource/resource.js

you need to import the proper module in your app :

var App = angular.module("App",["ngResource"]);

then use it in a "sane" way :

var User = App.factory("User",function($resource){
    var User = $resource('/api/user.json',{},{
       login:{method:"POST",isArray:true},
       logout:{method:"POST"}
    });
    return User;
    // use User in your controller or another service.
});

the doc is here : http://docs.angularjs.org/api/ngResource.$resource

Upvotes: 2

Related Questions