9

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'
            }
        }
    }
]);
2
  • 1
    So how are you calling the count method on the resource? Commented Jul 29, 2015 at 22:35
  • I don't see anything in the defaultHttpResponseTransform that would turn your string into an array. You'll need to show your code where you call the resource method and handle the response Commented Jul 29, 2015 at 23:56

1 Answer 1

7

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)...

  1. Opting to leverage the $http service instead

  2. Return your response in a wrapped object such as { 'collection': [20, 40, 60] }

  3. 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) }
        [...]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.