Invalid result from ngResource request with string

2020-06-28 03:35发布

问题:

i've the following ngResource defined:

angular.module('LicenseServices', ['ngResource']).
    factory('License', function ($resource) {
        return $resource('api/licenses/:id', { id: '@id' }, {
            //...
            'getLicenseTypes': { method: 'GET', url: 'api/licenses/types', isArray: true }
        });
    });

The result of the GET request is:

["Trial","Standard"]

But using the resource in a controller:

$scope.licenseTypes = License.getLicenseTypes()

i get the following result:

licenseTypes: 
[ undefined, { 
0: S
1: t
2: a
3: n
4: d
5: a
6: r
7: d
 } ]

I'm using AngularJS 1.1.4 with Chrome.

Whats wrong with my resource definition?

回答1:

There is really not much point in using $resource for data structures like those. The thing is that $resource works great for RESTful endpoint where all the HTTP verbs are used. To make this easy AngualarJS extends incoming objects with convenience methods ($save, $delete etc.). If you return primitives there is no room for extension and one of the major benefits of $resource is gone.

In short: if you are just after fetching data $resource is an overkill IMO, stick with the $http instead.