簡體   English   中英

Angularjs $ interval返回fn不是函數

[英]Angularjs $interval returns fn is not a function

我想檢查cookie是否存在$ interval。 我在頁面加載時調用$ interval。 此調用會定期拋出錯誤:

> TypeError: fn is not a function
>     at callback (angular.js:12516)
>     at Scope.$eval (angular.js:17444)
>     at Scope.$digest (angular.js:17257)
>     at Scope.$apply (angular.js:17552)
>     at tick (angular.js:12506)

我真的不明白為什么。

這是我的代碼:

angular.module("appModule")
.controller("loginController", ["$scope", "$http", "$window", "$document", "$interval", "$cookies",
    function ($scope, $http, $window, $document, $interval, $cookies) {

    var stopInterval;
    $scope.CheckLoginCookie = function () {

        if ($cookies.get("Login") != null) {

            if (angular.isDefined(stopInterval)) {
                $interval.cancel(stopInterval);
                stopInterval = undefined;
            }

            $window.location.href = $scope.UrlNotes;
        }
    }

    $scope.Repeat = function ()
    {
        stopInterval = $interval($scope.CheckLoginCookie(), 1000);
    }
}]);

從$ document.ready調用代碼:

$document.ready(function () {      
    $scope.Repeat();
})

您添加了函數的結果而不是函數本身。 調用$scope.CheckLoginCookie()將返回undefined ,但$interval需要回調。

$interval($scope.CheckLoginCookie, 1000);

如果函數需要參數,只需使用它:

$interval(function() {
    $scope.CheckLoginCookie(param1, param2);
}, 1000);

我使用了Str的答案,它對我有用。 在我的控制器中,我希望每隔5分鍾刷新一次圖表,最后我添加了這個。 首先我調用我的refreshChart函數,因此圖表會在第一次加載或刷新頁面時立即加載。 然后我在我的控制器中調用$ scope.Repeat函數。 我的圖表每5分鍾刷新一次,是的!

    $scope.Repeat = function () {
        intervalPromise = $interval(function () {
            $scope.refreshChart();
        }, 300000);
    }

    $scope.refreshChart();

    $scope.Repeat();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM