Search code examples
angularjsangular-resource

Get specific fields using an Angular $resource


Is there a way to get specific fields with an Angular $resource? Without creating a new REST endpoint that specifically returns the fields I care about?

For example, let's say I have a Person $resource and it is bound to a REST endpoint where GET returns the full Person object. Is there a way to specify 'select' params (for just _id & names) in addition to the query params? Or do I have to create a PatientNames $resource?


Solution

  • its all about how you configure your endpoint if you $resources can be parameterized and take key, value pairs to specify query query string params and path based on the url template you used to create the resource ie

      var Person=$resource('/url/person/:id')
      Person.get({fields:['name','ssn']})
    

    this will issue a get request to

      'url/person?fields=name&fields=ssn
    

    now on your server you can take the fields array in the query string and use it to return those fields and if no fields array is present you return them all