Virtual attributes in ngResource

angularjs, ngresource

Solution

You could try intercepting the response from the Person resource and augment the response. Like this:

app.factory('Person', ['$resource', function($resource) {
  function getFullName(){
      return this.name + ' ' + this.surname;
  };

  return $resource('api/path/:personId', {
    personId: '@_id'
      }, {
        update: {
          method: 'PUT'
        },
        'get': {method:'GET', isArray:false,interceptor:{
              'response': function(response) {
                  response.fullname = getFullName; //augment the response with our function
                  return response;
               }
         }}
      });
}]);

Problem

Is it possible to add virtual attributes to a ngResource? I created a service like this ``` app.factory('Person', ['$resource', function($resource) { return $resource('api/path/:personId', { personId: '@_id' }, { update: { method: 'PUT' } }); }]) ``` `Person` has an attribute `name` and an attribute `surname`. I want to retrive the `fullname` by adding a virtual attribute `fullname` returning `resource.name + resource surname`. I know I could add it in the controller, but adding it to the service it will make it much more portable. I tried something like this ``` app.factory('Person', ['$resource', function($resource) { return $resource('api/path/:personId', { personId: '@_id' }, { update: { method: 'PUT' }, fullname: function(resource){ return resource.name + ' ' + resource.surname; } }); }); }]) ``` but it doesn't work.

Original source