javascript Angularjs 资源 query() 结果数组作为属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16387202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:27:04  来源:igfitidea点击:

Angularjs resource query() result array as a property

javascriptangularjsangular-resource

提问by mojzis

I like the way the query()method returns an array of resources, which can be saved to the server again.
I am trying to use Angular against the Drupal RestWSmodule, which returns an object with several "meta" properties and a property called list where the actual data are stored. Is there please a way of telling the resource to take that array instead ?

我喜欢该query()方法返回资源数组的方式,这些资源可以再次保存到服务器。
我正在尝试对Drupal RestWS模块使用 Angular,该模块返回一个具有多个“元”属性的对象和一个名为 list 的属性,其中存储了实际数据。有没有办法告诉资源取而代之?

Example : GET author.jsonreturns :

示例:GET author.json返回:

first: "http://dgh/author?page=0"
last: "http://dgh/author?page=0"
list: [{id:1, type:author, uid:{uri:http://dgh/user/1, id:1, resource:user}, created:1367770006,…},…]
self: "http://dgh/author"

回答by Aleksander Blomsk?ld

With the latest Angular version (1.1.2 or later), you can configure the resource with a transformResponse:

使用最新的 Angular 版本(1.1.2 或更高版本),您可以使用 transformResponse 配置资源:

var MyResource = $resource(
    '/author.js',
    {},
    {
        'get': {
            method: 'GET',
            transformResponse: function (data) {return angular.fromJson(data).list},
            isArray: true //since your list property is an array
        }
    }
);