Search code examples
angularjsangular-resourceangular-services

How to process parameter sent through $resource get?


In the controller:

MyService.get({queryParameter:'MyQueryParameter'}).$promise.then(function(result){
  return result; 
};

In the service I have:

$resource('/api/path',{
  queryParameter: (function process(queryParameter) {
    //process queryParameter
    return processed_query_parameter;
  })('@queryParameter'),
});

In an attempt to simulate what it's normally done:

queryParameter: '@queryParameter'

However this doesn't seem to work. Nor this:

$resource('/api/path',{
   queryParameter: (function process(queryParameter) {
     //process queryParameter
     return processed_query_parameter;
   })(@queryParameter),
});

Or this:

$resource('/api/path',{
   queryParameter: (function process(queryParameter) {
     //process queryParameter
     return processed_query_parameter;
   })(queryParameter),
});

So, how do I access the passed parameter, and process it before I assign it?


Solution

  • try something like this:

    angular.module('app').service('MyService', ['$resource', function MyService($resource) {
    var rs = $resource('http://localhost:8080/api');
    
        rs.getPersonalized(params){
            var customParams = {
                queryParameter: params.id
            }
            return rs.get(customParams);
        }
    
        return rs;
    }]);