【发布时间】:2015-09-05 10:35:54
【问题描述】:
我有这个service,它检查后端是否有新数据。它工作正常。但问题是我无法使用$watch 或promise 将数据从服务获取到控制器。
服务
.service('notificationPollService',function($q, $http, $timeout){
var notification={};
notification.poller = function(){
return $http.get('some/routes/')
.then(function(response) {
$timeout(notification.poller, 1000);
if (typeof response.data === 'object') {
return response.data;
} else {
return $q.reject(response.data);
}
}, function(response) {
$timeout(notification.poller, 5000);
return $q.reject(response.data);
});
}
notification.poller();
return notification;
})
在控制器中观看
$scope.$watch('notificationPollService.poller()', function(newVal){
console.log('NEW NOT', response) // does nothing either.
}, true);
控制器中的承诺
notificationPollService.poller().then(function(response){
console.log("NEW NOTI", response) // not logging every poll success.
});
有没有我错过的方法来解决这个问题?还是我只是做错了什么?
【问题讨论】:
标签: javascript angularjs long-polling watch