Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Right, I'm using $resource and query to access a RESTful interface which gives a response in the following format:

record (array[RecordResponse], optional)

eg:

{
  "record": [
    {
      "countryID": 732,
      "countryName": "Western Sahara",
      "countryISO": "EH",
      "countryISO3": "ESH"
    },
    {
      "countryID": 4,
      "countryName": "Afghanistan",
      "countryISO": "AF",
      "countryISO3": "AFG"
    },
    {
      "countryID": 8,
      "countryName": "Albania",
      "countryISO": "AL",
      "countryISO3": "ALB"
    }
  ]
}

my current $resource query looks like this:

return $resource('http://mydatasource/countries?fields=countryID%2CcountryName%2CcountryISO%2CcountryISO3', {}, {
  query: {method:'GET', isArray:false}
})

In my controller I get access the resource using:

  $scope.places = Countries.query();

Which I can then put in a nice list in my view and do stuff, for example:

<tr ng-repeat="country in places.record">
  <td>{{country.countryName}}</td>
</tr>

Great no problems there!

However, I'm running in to problems when I'm trying to perform filters etc with the returned data in the controller. I need to get the JSON objects out of the records array but I'm not sure how to do that outside of the view!

Any pointers would be much appreciated.

Thanks

share|improve this question

1 Answer 1

up vote 0 down vote accepted

As per usual no sooner do I submit a question I think I come up with a solution.....

I updated the $resource to:

return $resource('http://mydatasource/countries?fields=countryID%2CcountryName%2CcountryISO%2CcountryISO3', {}, {
      query: {
        method:'GET',
        isArray:true,
        transformResponse: function(data, header) {
            var wrappedobj = angular.fromJson(data);
            return wrappedobj.record;
            }
        }

    })

Hopefully this might help someone else out there too!

share|improve this answer
    
Do you have any idea how to display data with ng-repeat if I pull data from a DRF page with JSON array? Here's my question: link –  MiniGunnR Jun 4 at 12:59
    
Can you post a plunkr or jsfiddle of your code and I'll try help! –  Antony Smith Jun 5 at 13:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.