【发布时间】:2014-08-28 00:16:05
【问题描述】:
我有一个 angularjs 应用程序可以很好地与 django-rest 配合使用,但通过引入分页遇到了问题。我有一个休息服务和控制器,如下所示
// restservices.js
// API call for all images in an event
services.factory('ImageEvent', function ($resource) {
return $resource(rest_api + '/rest/image/?event_id=:eventId&format=json', {}, {
query: { method:'GET', params:{eventId:''}, isArray:true}
})
});
// controllers.js
// all Images in an event
.controller('ImageEventCtrl', ['$scope', '$stateParams', 'ImageEvent', function($scope, $stateParams, ImageEvent) {
$scope.images = ImageEvent.query({eventId: $stateParams.eventId}, function(images) {
});
}])
这将返回以下 json
[
{
"id": 13,
"title": "01-IMG_4953.JPG",
},
{
"id": 14,
"title": "02-IMG_4975.JPG",
},
{
"id": 15,
"title": "03-IMG_4997.JPG",
}
]
但是,如果我打开 django-rest 分页,它会返回以下 json:
{
"count": 3,
"next": "/rest/image/?event_id=1&page=2",
"previous": null,
"results":
[
{
"id": 13,
"title": "01-IMG_4953.JPG",
},
{
"id": 14,
"title": "02-IMG_4975.JPG",
}
]
}
此更改导致以下控制台错误,并且一切都无法正常工作:
错误:[$resource:badcfg] 资源配置错误。预期的响应包含一个数组但得到一个对象
将 restservice 更改为 isArray:false 没有任何区别。是否可以重写我的控制器以应对此问题,并且在完美的情况下还可以公开计数、下一个和上一个链接?
谢谢
【问题讨论】:
标签: json django angularjs rest pagination