我正在尝试为应用程序设置轮询,并且我有一个实现,它做了最低限度的实现,在那里它似乎最初运行,获得第一个响应,但未能调用$timeout并抛出异常(见下文)。我不知道为什么它不会正确轮询,它看起来应该工作,API说$timeout需要一个函数…什么好主意吗?
如果我注释掉第65行$timeout(…);我没有得到错误。
Error: [$http:badreq] Http request configuration must be an object. Received: function ()
http://errors.angularjs.org/1.4.7/$http/badreq?p0=function%20()
at angular.js:68
at $http (angular.js:10189)
at scoreboard-service.js:65
at processQueue (angular.js:14745)
at angular.js:14761
at Scope.$eval (angular.js:15989)
at Scope.$digest (angular.js:15800)
at Scope.$apply (angular.js:16097)
at done (angular.js:10546)
at completeRequest (angular.js:10744)
.run
中的轮询工厂和初始化。
/**
*
*/
.factory('ScoreboardPollFactory', ['ConfigSettings', 'ScoreboardFactory', '$http', '$timeout',
function (ConfigSettings, ScoreboardFactory, $timeout) {
var pollingTimeout = ConfigSettings.pollingTimeout;
var data = {response: {}, calls: 0};
var poll = function (courtId) {
var courtId = 1; // testing app so hardcoded for now till API is ready
console.log(ScoreboardFactory); // debug: has correct factory object, and never makes it back here after hitting $timeout
ScoreboardFactory.getCourtScore(courtId).then(function (response) {
data.response = response.data
data.calls++;
console.log(response); // debug: contains first response
console.log(data);
console.log(poll); // debug: outputs poll function
$timeout(function() { // <-- this is line 65 from error
poll(courtId);
}, pollingTimeout);
});
}
poll();
return {
data: data
};
}])
/**
*
*/
.run(['ScoreboardPollFactory', function (ScoreBoardPollFactory) {
}]);
这是你的问题(排成一行,这样你就可以看到发生了什么)
.factory('ScoreboardPollFactory',
['ConfigSettings', 'ScoreboardFactory', '$http', '$timeout',
function (ConfigSettings, ScoreboardFactory, $timeout) {
将$http
服务作为$timeout
注入。
我建议使用像ng-annotate这样的构建工具来创建依赖注入数组,这样就可以避免遇到这样的问题。