Reputation: 604
I use ngResource to query a JSON service and per default Angular parses the response. Unfortunately I'd also like to have access to the raw response string.
Here is a snippet of what I'd like to achieve:
var app = angular.module('plunker', ['ngResource']);
app.factory('DateResource', function($resource, $http) {
var raw = null,
resource = $resource('http://date.jsontest.com/', {}, {
query: {
method: 'GET',
transformResponse: [
function(data) {
raw = data;
return data;
}
].concat($http.defaults.transformResponse).concat([
function(data) {
data.$raw = raw;
return data;
}
])
}
});
return resource;
});
app.controller('MainCtrl', function($scope, DateResource) {
DateResource.query({}, function(response) {
console.log('response:', response); // should be parsed JSON object
console.log('response.$raw:', response.$raw); // should be raw JSON string
});
});
Checkout the full example: http://plnkr.co/edit/RSwrRQFo1dEGDkxzRTcF
This implementation using two transformRequest
functions and a raw
variable in the parent scope is not really a good idea, since the calls might be asynchronous...
Is there a possibility to identify the response, so I can remember the raw content per response and attach it later in the second transformResponse
? Or do you know another solution?
Upvotes: 3
Views: 4664
Reputation: 2240
You can do that by overriding the transform defaults and use the angular.fromJson(rawData)
resource = $resource('http://date.jsontest.com/', {}, {
query: {
method: 'GET',
transformResponse: transformGet
}
});
function transformGet(json, headerGetter) {
var fromJson = angular.fromJson(json);
fromJson.json = json ;
return fromJson;
}
Plunker: http://plnkr.co/edit/uIIdKAeUwRN4ThUlMsvd
Hope that helped
Upvotes: 4
Reputation: 262
var app = angular.module('plunker', ['ngResource']);
app.factory('DateResource', function($resource, $http) {
var raw = null,
resource = $resource('http://date.jsontest.com/', {}, {
query: {
method: 'GET',
}
});
return resource;
});
app.controller('MainCtrl', function($scope, DateResource) {
DateResource.query({}, function(response) {
console.log('response:', response); // should be parsed JSON object
console.log('response.$raw:', angular.toJson(response)); // should be raw JSON string
});
});
see below link for clear understanding:
https://docs.angularjs.org/api/ngResource/service/$resource
Upvotes: -1