10
'use strict';
angular.module('rmaServices', ['ngResource'])
    .factory('rmaService', ['$resource',
        function ($resource) {
            return $resource(
                   '/RMAServerMav/webresources/com.pako.entity.rma/:id',

                    {},
                   {
                      delete: { method: 'DELETE', params: {id: '@rmaId'}}, 
                      update: { method: 'PUT', params: {id: '@rmaId'}},
                      //RMAServerMav/webresources/com.pako.entity.rma/0/3
                      findRange:{method: 'GET', params:{id:'@rmaId'/'@rmaId'}}
                    });
        }]);

RMAServerMav/webresources/com.pako.entity.rma/0/3

This is correct way to use findRange REST service. This one returns the rmaID from 1 to 4, but how can I use this from controller and what is the correct syntax in service?

In controller I would like to use it something like that:

$scope.rmas = rmaService.findRange({id:'0'/'3'});

but this is not working.

3 Answers 3

24

You can override url, Read $resource docs

url – {string} – action specific url override. The url templating is supported just like for the resource-level urls.

In resource declaration

findRange:{ 
    url: '/RMAServerMav/webresources/com.pako.entity.rma/:id/:to', 
    method: 'GET', 
    params:{ 
        id:'@id', 
        to: '@to'
    }
}

In Controller

$scope.rmas = rmaService.findRange({id:0, to: 3});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was it. I really don't like Google document about AngularJS. Examples are too simply and documentation is....not for me or they are too mixed. Basically I tried this but I didn't change the url. How about if you have to get from two tables (joins etc)? Is it possible?
0

I would prefer shorter way of defining parameters. Here is a complete example.

We have here 2 params :latitude and :longitude they defined only in the URL. Method get is already defined by ngResource

angular.module('myApp', ['ngResource'])
  .controller('myCtrl', function (ReverseGeocoderResource) {
    ReverseGeocoderResource.get({longitude: 30.34, latitude: 59.97}).$promise.then(function (data) {
      console.log(data.address.road + ' ' + data.address.house_number);
    })
  })
  .factory('ReverseGeocoderResource', function ($resource) {
    return $resource('https://nominatim.openstreetmap.org/reverse?format=json&lat=:latitude&lon=:longitude&zoom=18&addressdetails=1&accept-language=ru');
  });

Comments

-1

Try changing your service using it in the controller like this:

'use strict';
angular.module('rmaServices', ['ngResource'])
    .factory('rmaService', ['$resource',
        function ($resource) {

          var service ={}

          service.rma = function(){ // name it whatever you want

                return $resource(
                   '/RMAServerMav/webresources/com.pako.entity.rma/:id',

                    {},
                   {
                      delete: { method: 'DELETE', params: {id: '@rmaId'}}, 
                      update: { method: 'PUT', params: {id: '@rmaId'}},
                      //RMAServerMav/webresources/com.pako.entity.rma/0/3
                      findRange:{method: 'GET', params:{id:'@rmaId'/'@rmaId'}}
                    });
          };
          return service;
}]);


//in controller
rmaService.rma()
  .then(function(resource){
    $scope.rmas = resource.$findRange({id:'0'/'3'});
  });

I have no idea if this will work BTW, because I have no use ngResource but that is how I code my factory services.

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.