Fre*_*kyB 6 javascript angularjs
我想检查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)
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么.
这是我的代码:
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);
}
}]);
Run Code Online (Sandbox Code Playgroud)
从$ document.ready调用代码:
$document.ready(function () {
$scope.Repeat();
})
Run Code Online (Sandbox Code Playgroud)
str*_*str 12
您添加了函数的结果而不是函数本身.调用$scope.CheckLoginCookie()将返回undefined,但$interval预计会回调.
$interval($scope.CheckLoginCookie, 1000);
Run Code Online (Sandbox Code Playgroud)
如果函数需要参数,只需使用它:
$interval(function() {
$scope.CheckLoginCookie(param1, param2);
}, 1000);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4986 次 |
| 最近记录: |