Question

Ok, so I think I'm missing something basic here but I couldn't figure it out reading the docs and other examples. I have a resource in a factory like this:

loteManager.factory('Lotes', function($resource) {
  return $resource('./api/lotes/:id',{ id:"@id" }, {
     get:  {method:'GET', isArray:true}
   });
});

And my controller:

loteManager.controller('LoteCtrl',
  function InfoCtrl($scope, $routeParams, Lotes) {
    Lotes.get(function (response){
      console.log(response);
    });
});

It works when I define the id manually like this $resource('./api/lotes/21' so I think the problem is passing the id to the factory but I already tried adding params:{id:"@id"} but that didn't work either.

Was it helpful?

Solution

You need to pass in the id.

Something like this:

loteManager.controller('LoteCtrl',
  function InfoCtrl($scope, $routeParams, Lotes) {
    Lotes.get({id: $routeParams.loteId}, function (response){
      console.log(response);
    });
});

...assuming you have a route defined something like this:

$routeProvider.when('/somepath/:loteId, {
    templateUrl: 'sometemplate.html',
    controller: LoteCtrl
});

Per the documentation:

var User = $resource('/user/:userId', {userId:'@id'});
var user = User.get({userId:123}, function() {
  user.abc = true;
  user.$save();
});

OTHER TIPS

I think your problem is you are saying there are parameters for your 'get' method (id), but you aren't giving the method 'get' an id when you make your call at Lotes.get(..)

So, I think, your method call should be something along the lines of

Lotes.get({id: SOME_Id}, function(response){
    // ...do stuff with response
});

I'm not totally sure on that syntax, because personally I prefer the $q service as it provides much more flexibility, but that is what's going wrong with your code in general, you aren't giving your method the parameter it needs (an id).

Also, remember to utilize your Angular's $timeout service, as you are making an async call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top