控制器内的控制台服务响应

时间:2015-06-24 11:57:27

标签: angularjs angularjs-service angular-promise

我已经用take参数编写了一个服务,根据该参数,它响应了http请求的响应。

    this.getPaymentDueDetails=function(date){
    this.getfromRemote('paymentdue/'+btoa(date))
    .success(function(response){
        return response;
    })
    .error(function(response){
        return false;
    })
}

getfromRemote是我提出http请求的另一项服务

现在我想在我的控制器功能

中获得此服务调用的响应
 $scope.callDueReports=function(blockNum){
       var data;
       data=myAngService.getPaymentDueDetails('2015-04-20');
console.log(data);
        }

很明显,我在网页最初加载时不会得​​到任何数据,但我想得到getPaymentDueDetails的结果。

1 个答案:

答案 0 :(得分:1)

请修改您的服务以返回以下承诺。

this.getPaymentDueDetails = function(date) {
    return this.getfromRemote('paymentdue/' + btoa(date));
};

在控制器中检查承诺是否已解决。

$scope.callDueReports = function(blockNum) {
    var data;

    myAngService.getPaymentDueDetails('2015-04-20').then(function(dataFromService) {
            data = dataFromService;
            console.log(data);
        })
        .catch(function(response) {
            console.error('error');
        });
};