Dennis A. Boanini
Dennis A. Boanini

Reputation: 497

$resource return an array of char instead of string like the answer of the server

I'm using a service in angularJs that retrieve information from the server. Using postman if i call this url http://localhost:8080/api/public/v1/indirizzo/zona?stradaId=5786&civico=53r the server answer with a string 0480170061900.

My service is

var resourceUrl =  'api/public/v1/indirizzo/:type';

    return $resource(resourceUrl, {}, {
        civico: {
            method: 'GET',
            isArray: false,
            params: {
                type: 'civico'
            },
            cache: true
        },
        autocomplete: {
            method: 'GET',
            isArray: true,
            params: {
                type: 'autocomplete'
            },
            cache: true
        },
        zona: {
            method: 'GET',
            isArray: falsem,
            params: {
                type: 'zona'
            },
            cache: true,
            transformResponse: []
        }
    });

In my controller I call 'zona' in this manner

function searchZona(stradaId, numeroCivico){
            vm.stradaId = stradaId;
            return Indirizzo.zona({stradaId: stradaId, civico: numeroCivico}).$promise.then(function(data){
                vm.zona = data;
                console.info("ZONA ID: ", vm.zona);
                return data;
            }, function(error){
                AlertService.error(error.data.message);
            });
        }

everything work correctly but vm.zona have this information

{"0":"0","1":"4","2":"8","3":"0","4":"1","5":"7","6":"0","7":"0","8":"6","9":"1","10":"9","11":"0","12":"0"} 

why? Is possible to transform this in a string like the answer of the server?

Upvotes: 1

Views: 807

Answers (1)

georgeawg
georgeawg

Reputation: 48968

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data.

The AngularJS framework uses angular.copy to populate the reference. If the source is not an object or array, nothing is copied.

The transformResponse function can transform a string response, but it must transform the response to either an array or an object in order to work with angular.copy

Upvotes: 1

Related Questions