Make angularjs $resource return array of [OO] objects

前端 未结 2 538
不思量自难忘°
不思量自难忘° 2020-12-19 12:07

How to make angularjs $resource return an array of objects derived/prototyped from specified domain object?

Here is an example on http://plnkr.co/edit/AVLQItPIfoLwsg

2条回答
  •  忘掉有多难
    2020-12-19 13:04

    You can manipulate your data using transformResponse option in your resource service definition (make sure to set isArray to true):

    angular.module('services', ['ngResource']).
        factory("yourService", function ($resource) {
            return $resource(
                '/custom/url', {}, {
                get: {
                    method: 'GET',
                    isArray: true,
                    transformResponse: function(data, headers){
                        //
                        // transform to array of objects 
                        return data;
                    }
                }
            }
        );
    });
    

提交回复
热议问题