How to convert an Angular $ resource object to a regular object without methods

I'm starting to learn the AngularJS $ resource and notice that the $ resource object has several methods (see examples below) attached to my data downloaded from the server. How to remove these methods and convert the object to a regular (array) object?

__proto__: Resource $delete: function (params, success, error) {$get: function (params, success, error) {$query: function (params, success, error) {$remove: function (params, success, error) {$save: function (params, success, error) {constructor: function Resource(value) {toJSON: function () {__proto__: Object 

For example, I'm trying to send a POST request, including some data about the key value, using $ resource.save, but these proto elements in the array somehow cause the data to become "undefined" when passing $ .param (data) to factory. I could do the same using $ http with ease, but I want to know $ resource. Thanks!

Inside the controller

  $scope.ok = function () { $scope.entry = new calEntry(); $scope.entry.data = data // data is $resource object including _proto_ items $scope.entry.$save( function(){ toaster.pop('success','Message','Update successfully completed.'); }); }; 

Factory

 myApp.factory("calEntry",['$resource','$filter', function($resource, $filter) { return $resource("/griddata/", {}, { save: { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}, transformRequest: function(data, headersGetter) { return $.param(data); // data is undefined when it reaches here } } }); }]); 
+6
source share
1 answer

Try the toJSON function, it will retrieve data and remove additional properties.

+8
source

Source: https://habr.com/ru/post/980816/


All Articles