AngularJS Poller投掷

时间:2015-10-27 00:02:06

标签: javascript angularjs polling

我正在尝试为应用程序设置轮询,并且我有一个实现,它在最初运行时最小化,获得第一个响应,但无法调用$ 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) {
}]);

1 个答案:

答案 0 :(得分:2)

这是你的问题(排成一列,所以你可以看到发生了什么)

.factory('ScoreboardPollFactory',
            ['ConfigSettings', 'ScoreboardFactory', '$http', '$timeout',
    function (ConfigSettings,   ScoreboardFactory,   $timeout) {

您正在将$http服务注入$timeout

我建议使用像ng-annotate这样的构建工具来创建依赖注入数组,这样就可以避免遇到这样的问题。