【发布时间】:2015-01-15 21:29:49
【问题描述】:
我想使用 Restangular 轮询后端,直到满足条件。我正在使用 Angular Poller 进行轮询 (https://github.com/emmaguo/angular-poller)。它返回一个承诺。
下面的代码正在正确执行轮询,但我无法访问返回的数据。 then 中的 console.log 未打印。
这里发生了什么?
var myPoller = poller.get(Restangular.one('batches',$routeParams.panel_id), {
action: 'get',
delay: 1000,
arguementsArray: []
});
myPoller.promise.then(function(batch){
$scope.running = batch.batch_status;
console.log('Status: ' + batch.batch_status);
if (batch.batch_status === 'complete'){
myPoller.stop();
}
});
更新
Angular Poller 的文档中提到了回调。
https://github.com/emmaguo/angular-poller#customize-restangular-poller
myPoller.promise.then(null, null, callback);
【问题讨论】:
-
你的空值,空值在哪里?
-
我不太清楚回调应该如何工作。我可以创建一个名为“回调”的函数,console.logs 从那里开始工作,但我如何访问返回的数据?
-
您正确地指出 thge 文档说
....then(null, null, callback)但您自己的代码是....then(callback)。那么你的 null,nulls 在哪里? -
null,nulls btw 是成功和错误函数,如角度文档中的The Promise API 中所述。 then 函数定义为
then(successCallback, errorCallback, notifyCallback)。 -
谢谢@Patrick。我就是这么想的,我尝试在我的原始代码中调用successCallBack,但我猜我的语法不正确。
标签: angularjs promise restangular