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

console service response inside controller

我写了一个带有 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 int 的结果。

请将您的服务修改为return如下承诺。

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');
        });
};