Question

Im trying a service returning multiple resources to be injected in a controller, but calling resource "query" method I get an error that method doesn't exist. below will find the code example of what I want to do. I do method call at controller side because there is additional logic when promise success. Is there a way to do that?

//Service

ImageRepo.factory('ServerCall', ['$resource', function ($resource) {
return {
    Image: function () {
        return $resource('/api/image/:id', { id: '@id'});
    },
    Meta: function () {
        return $resource('/api/metadata/:id', { id: '@id'});
    }
}
}]);

//Controller function

var ImageCtrl = function ($scope, ServerCall) {
...
$scope.metadefsearch = function () {
    ServerCall.Meta.query().$promise.then(function (metasdef) {
        $scope.metasdefinition = metasdef;
       //Some additional logic.....
    });
  };
};
Was it helpful?

Solution

Let Meta and Image be objects instead of functions:

ImageRepo.factory('ServerCall', ['$resource', function ($resource) {
  return {
    Image: $resource('/api/image/:id', { id: '@id'});,
    Meta: $resource('/api/metadata/:id', { id: '@id'}) 
  }; 
}]);

OTHER TIPS

Since your two properties are methods you should do this:

ServerCall.Meta().query().$promise.then(function (metasdef) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top