Muhammad Nasir
Muhammad Nasir

Reputation: 2204

Transforming Requests and Responses in AngularJS

What does Transforms do in angularjs http serivce. Here is how it is explained in documentation with the example below for custom transformation.

Both requests and responses can be transformed using transformation functions: transformRequest and transformResponse. These properties can be a single function that returns the transformed value (function(data, headersGetter, status)) or an array of such transformation functions, which allows you to push or unshift a new transformation function into the transformation chain

function appendTransform(defaults, transform) {

// We can't guarantee that the default transformation is an array
  defaults = angular.isArray(defaults) ? defaults : [defaults];

  // Append the new transformation to the defaults
  return defaults.concat(transform);
}

$http({
  url: '...',
  method: 'GET',
  transformResponse: appendTransform($http.defaults.transformResponse, function(value) {
    return doTransform(value);
  })
});

What transform will do ? It is not clearing to me from documentation can some Explain ?

Thanks for help.

Upvotes: 1

Views: 1564

Answers (1)

Jenna Leaf
Jenna Leaf

Reputation: 2462

Check this out. This should give you a picture of how to use and why it is there as one of the $http's services

transforming your response using services.transformer.ApiResponse

so you can re-formulate the function's JSON.parse(data) to get what you want your response to have!

Upvotes: 2

Related Questions