为了处理分页等,我决定创建一个名为ApiResponse的Object包装器,如下所示:
public class ApiResponse<T> {
List<T> objectList;
List<?> nestedElements;
int currentPage;
int offset;
// etc...
}
在Angular中我构建了一个服务如下:
App.factory('Apartment', ['$resource', function ($resource) {
return $resource(
'http://localhost:8080/Kamienica/api/v1/apartments/:id.json',
{id: '@id'},
{
'query': {method:'GET', isArray:false},
'update': {method: 'PUT'}
}
);
}]);
在控制器中我有:
self.fetchAll = function() {
var res = Apartment.query();
console.log('===== console.log(res); =====');
console.log(res);
console.log('===== console.log(res.objectList); =========');
console.log(res.objectList);
};
当我打印'res'时,我正确地得到了对象但是如果想要达到它的变量,我会得到'undefined',你可以在屏幕截图中看到
以前在Spring控制器中我返回了一个List&lt; Apartment&gt; 相反,如果ApiResponse&lt; Apartment&gt;它运作良好。
底线是我如何达到objectList和对象存储的其他变量?
答案 0 :(得分:0)
当您致电Apartment.query();
时,会在后台从服务器获取数据。当您致电console.log(res.objectList);
时,它没有属性objectList
,因为数据尚未提供。