响应类型为text/plain的Angular资源总是生成一个字符串数组

nuc*_*ote 9 javascript arrays angularjs angularjs-service angularjs-resource

我创建了从休息服务接收记录数的资源作为文本.Angular从回答中为每个字符组成一个数组.例如,如果休息答案20,angular将生成数组[2,0].我可以在不转换响应或使用的情况下修复它$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'
            }
        }
    }
]);
Run Code Online (Sandbox Code Playgroud)

scn*_*iro 8

Angular难以检索字符串列表$resource.你有一些选择(由于你的问题中的限制,建议2是你可能想要的)...

  1. 选择利用该$http服务

  2. 在包装对象中返回您的响应,例如 { 'collection': [20, 40, 60] }

  3. 通过定义的属性转换响应和访问,例如data.collection.转换您的回复的示例可能包括......


return $resource('/rest/records/:id', {}, {
    count: { 
        method:'GET',
        transformResponse: function (data) {
            return { collection: angular.fromJson(data) }
        [...]
Run Code Online (Sandbox Code Playgroud)