An angular resource with a text / plain response type always creates an array of strings

I created a resource that receives the number of records from the recreation service as a text one. Angular makes an array of responses for each character. For example, if rest answers 20 , Angular will make an array [2,0] . Can I fix this without changing the answer 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' } } } ]); 
+5
source share
1 answer

Angular is hard to get a list of strings with $resource . Some options that you have include (suggestion 2 is what you probably want due to limitations in your question) ...

  • Instead of using $http

  • Return your answer to the wrapped object, for example { 'collection': [20, 40, 60] }

  • Converting a response and access through a specific property, for example. data.collection . An example of converting your answer may include ...


 return $resource('/rest/records/:id', {}, { count: { method:'GET', transformResponse: function (data) { return { collection: angular.fromJson(data) } [...] 
+7
source

All Articles