Pregunta

I use a typeahead wich will request differently numericOnly and string search and since the result have the same structure, if the user look for a number I need to display obj.idnumber as main info, and if the user look for a name, I need to display obj.name as main info.

The main transform operation is to copy the right prop in a label prop in order to be display by the typeahead.

The documentation is not really clear, so can use $http.transformResponse to handle some proccessing for the data I received from $http?

thanks

Lionel

¿Fue útil?

Solución 2

This is the recommended way to use transformResponse. that way, you can use all the default transformResponse function already defined

function _transformResponse(){...}

var req= {
 url: url,
 method:"POST",
 transformResponse: $http.defaults.transformResponse.concat([_transformResponse])
}

Otros consejos

This is how I used transformResponse for a specific request. Hope it helps!

function transformResponse(data){
  // data is a raw json String, so you need to parse it before 
  try{
    data = angular.fromJson(data);
  } catch (e){
    console.error('Parsing error', e);
  }
  // transform data
  return data;
}

var req = {
  url: url,
  headers:{
    "Content-Type": "application/json",
  },
  method: "POST",
  data: data
}
// Register the transformResponse Handler
req.transformResponse = [transformResponse];
return $http(req);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top