我正在使用基于这个答案的方法:Server polling with AngularJS
但是当我有多种轮询方法时,如何设置此更新?
这是我的服务片段:
function pollingService($resource) {
return {
methodA: $resource(window.rootUrl + 'api/methodA', { para: '@para1' }, {
query: { method: 'GET', params: {}, isArray: false }
}),
methodB: $resource(window.rootUrl + 'api/methodB', {}, {
query: { method: 'GET', params: {}, isArray: false }
})
}
};
那么我怎样才能设置tick方法来轮询这2个方法并且只创建1个轮询循环呢?
(function tick() {
$scope.method1 = pollingService.methodA.query(function () {
$timeout(tick, $scope.refreshRate);
});
$scope.method2 = pollingService.methodB.query(function () {
$timeout(tick, $scope.refreshRate);
});
})();
最佳答案
您可以使用 Promise $q.all
函数:
var myTickFunc = function() {
return $q.all([pollingService.methodA.query().$promise, pollingService.methodA.query().$promise)
.then(function(result){
//Setup timer again
});
关于javascript - AngularJS 使用 ng-resource 和多种服务器轮询方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34134666/