本文介绍了Restangular:用的GetList含有对象的嵌入式阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我AngularJS项目中,我尝试使用Restangular的GetList方法,但由于API响应不直接数组它返回一个错误,但包含数组的对象。

In my AngularJS project I'm trying to use the Restangular getList method but it's returning an error because the API response is not directly an array but an object containing an array.

{
  "body": [
    // array elements here
  ],
  "paging": null,
  "error": null
}

该Restangular错误消息是:

The Restangular error message is:

Error: Response for getList SHOULD be an array and not an object or something else

是否有可能告诉Restangular,它的寻找的阵列是属性里面呢?

推荐答案

是的,请参阅Restangular文档。您可以配置Restangular像这样:

Yes, see the Restangular documentation. You can configure Restangular like so:

rc.setResponseExtractor(function(response, operation) {
    if (operation === 'getList') {
        var newResponse = response.body;
        newResponse.paging = response.paging;
        newResponse.error = response.error;
        return newResponse;
    }
    return response;
});

修改的:现在看来Restangular的API改变了,好了,而且,目前的方法是使用的 addResponseInterceptor 的。有些调整可能需要传递的函数。

Edit: It seems Restangular's API is now changed, for the better, and that the current method to use is addResponseInterceptor. Some adjustments might be needed to the function passed.

这篇关于Restangular:用的GetList含有对象的嵌入式阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 03:29