当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Getting object data from AngularJS $resource request, without the resource functions

问题描述:

I'm currently trying to export a CSV of items from an Angular $resource request in my controller Items.query() - the $resource requests return an array of Angular resource objects which have extra function properties like so:

Resource {_id: "540432e0f2ef60b1284d9187", example: "Stuff", __proto__: Resource $delete: function (params, success....

So on my CSV I get all the data I want but with extra messy columns from this __proto__ property. What would be the best way of separating out the data I need? Would I use a $promise.then() added to the query() and inside that modify the objects to be safe for CSV output?

Or would I add a function to the service itself for doing so? This is the api service:

angular.module('myApp', []).factory('Items', ['$resource', function($resource) {

return $resource('api/items/:id', {id: '@_id' }, {

update: {

method: 'PUT'

}

});

}]);

All the resources also have a property of __v: 0 but this doesn't appear in the CSV export, can someone explain why this isn't exported but the proto resource functions are?

Thanks!

网友答案:

Following @bmleite's advice I just created a new array using the resource object's data picking the properties I want. Something like:

Label.query().$promise.then(function(data) {
  var csvArray = [];
  for(var i = 0; i<data.length; i++) {
    csvArray[i] = {
      _id: data[i]._id,
      example: data[i].example,
      ......
    };
  }
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: