【问题标题】:Angular polling with Restangular使用 Restangular 进行角轮询
【发布时间】: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


【解决方案1】:

我解决了。对于其他想要将 Restangular 与 Angular Poller 一起使用的人,这是我的工作代码。

我坚持的部分是如何从 Restangular 访问返回的对象。结果你只是假设它正在被传递,并在回调中引用它。

然后可以通过查看返回的数据来构建轮询停止条件。

 var callback = function(batch) {
     console.log("in the callback " + batch.batch_status);
     $scope.running = batch.batch_status;
     if (batch.batch_status.hasOwnProperty('complete')){
        myPoller.stop();
     }

  }


  var myPoller = poller.get(Restangular.one('batches',$routeParams.panel_id), {
     action: 'get',
     delay: 1000,
     arguementsArray: []
  });

  myPoller.promise.then(null,null,callback);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-15
    • 2018-11-16
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多