I made resource that receive records count from rest service as text plain. Angular makes an array of each chars from answer. For example if rest answers 20
, angular will make array [2,0]
. Can I fix it without transforming response or using $http
?
var resource = angular.module('resource');
resource.factory('RecordResource', ['$resource',
function($resource) {
return $resource('/rest/records/:id', {}, {
count: {
method:'GET',
url: "/rest/records/count",
isArray: false,
responseType: 'text'
}
}
}
]);
Angular has difficulty retrieving a list of strings with $resource
. Some options you have include (suggestion two being what you likely want due to constraints in your question)...
Opting to leverage the $http
service instead
Return your response in a wrapped object such as { 'collection': [20, 40, 60] }
Transform the response and access through a defined property e.g. data.collection
. An example for transforming your response could include...
return $resource('/rest/records/:id', {}, {
count: {
method:'GET',
transformResponse: function (data) {
return { collection: angular.fromJson(data) }
[...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With