0

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

3
  • what's your error message , $resource is not included in the main script you need to import the angular-resource script : angular-resource.js Commented Jan 24, 2013 at 20:50
  • Hi @mpm, yes I have angular-resource.js imported. And $resource works. But since I have a function within the factory, I think the scope is messed up and I can't figure out how to allow that login function to be able to access $resource Commented Jan 24, 2013 at 22:19
  • then you should correct your question because i dont see query anywhere in your script. Furthermore $resource returns a resource object , you need to actually use the returned of object ot fire a request. Commented Jan 24, 2013 at 22:21

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.