angularjs poller notifyCallback状态代码

时间:2014-02-04 17:54:41

标签: angularjs

我们有什么方法可以访问响应状态?在notifyCallback中,在我看来它并不关心状态。

(function tick() {
  resource[action](params, function (data) {                                
    deferred.notify(data);
  }
};

1 个答案:

答案 0 :(得分:0)

经过大量研究后我了解到,如果我使用ng-resource,则无法访问服务器返回的数据,因为ng-resource不允许您访问标题,状态和数据。我做的是重写我的轮询服务。这很好用

registerController

poller.polling(generatePollerID(), resourceUrl, 1000, successCallBack, failureCallBack, 15);

var successCallBack = function (data, status, headers) {

if(status == 202 && data == "") {

// your success code here
}
};

var failureCallBack = function (data, status, headers) {


// your failure code here
}




pollerService 

var myApp = angular.module('poller', []);

myApp.factory('poller', function($http){

var pollList = {};
return {
startPolling: function(pollerName, url, pollingTime,  successCallBack, failureCallBack,
timeFrame) {

if (!pollList[pollerName]) {

counter = 0;

var poller = function() {

counter++;

$http.get(url).success(function (data, status, headers) {

if((status == "200") || (counter == timeFrame && status == "202") ) {

clearInterval(polls[name]);
delete pollList[pollerName];
counter = 0;

successCallBack(data, status, headers);
}
}).error(function (data, status, headers, config) {

clearInterval(pollList[pollerName]);
delete pollList[pollerName];
counter = 0;

failureCallBack(data, status, headers);
});
};
poller();
pollList[pollerName] = setInterval(poller, pollingTime);
}
}
};
});