在我的示例中,当我发送GET请求以获取带有数据的响应数组时,我在firefox的控制台中看到数组为空。我不知道错误在哪里。
这是我的代码:
//Controller:
$scope.changeDate = function (selFrom) {
vm.repos = CrudService.getRepoByDay(selFrom);
}
//Service (CrudService):
function CrudService(ResService) {
var service = {
getRepoByDay: getRepoByDay
};
return service;
function getRepoByDay(selTo) {
return ResourceService.test.query(
{ endDate: selTo },
successResponse,
errorResponse
);
}
function successResponse(response) {
return response;
}
}
//Service (ResService)
test: $resource(baseUrl + '/api/repodates', {
endDate: '@EndDate'
}, {})
//View
<tbody>
<tr ng-repeat="report in rc.repos>
<td ng-repeat="(key, value) in report">
{{ value }}
</td>
</tr>
</tbody>
有人知道我该做什么吗?
答案 0 :(得分:2)
$ resource是异步的,你必须在promise的回调中处理它:
return ResourceService.test.query(
{ endDate: selTo }
);
CrudService.getRepoByDay(selFrom).$promise.then(function(data){
vm.repos = data;
console.log(data);//log here not ouside the callback to check
});
使用$ q编辑样本:
var deferred = $q.defer();
ResourceService.test.query(
{ endDate: selTo }
).$promise.then(
function(data){deferred.resolve(data);},
function(rejection){deferred.reject(rejection);}
);
return deferred.promise;