How can I extend the constructor of an AngularJS resource ($resource)?

前端 未结 5 1092
醉话见心
醉话见心 2020-12-24 04:17

I have a model, defined using $resource, that I am successfully loading.

Each loaded instance is, as promised, an instance of the class I defined.

5条回答
  •  佛祖请我去吃肉
    2020-12-24 05:22

    transformResponse does the job. Consider example (I wanted to use Autolinker to format response content).

    return $resource('posts/:postId', {
        postId: '@_id'
    }, {
        get : {
            transformResponse : function(data) {
                var response = angular.fromJson( data );
                response.content = Autolinker.link(response.content);
                return response;
            }
        },
        update: {
            method: 'PUT'
    } });
    

提交回复
热议问题