0

我正在尝试在登录控件中编写空闲超时问题,但我得到了未定义的无法读取属性'watch'。

我正在尝试在 Tomcat Server 7 和 Angular 1.5 中执行以下代码

$scope.started = false;

function closeModals() {

    alert(1);
    if ($scope.warning) {
        $scope.warning.close();
        $scope.warning = null;
    }

    if ($scope.timedout) {
        $scope.timedout.close();
        $scope.timedout = null;
    }
}

$scope.$on('IdleStart', function() {
    closeModals();

    $scope.warning = $modal.open({
        templateUrl: 'warning-dialog.html',
        windowClass: 'modal-warning'
    });
});

$scope.$on('IdleEnd', function() {
    closeModals();
});

$scope.$on('IdleTimeout', function() {
    closeModals();
    $scope.timedout = $modal.open({
        templateUrl: 'timedout-dialog.html',
        windowClass: 'modal-danger'
    });
});

$scope.start = function() {
    console.log('start');
    closeModals();
    Idle.watch();
    $scope.started = true;
    alert("Hi")
};

$scope.stop = function() {
    console.log('stop');
    closeModals();
    Idle.unwatch();
    $scope.started = false;
};

nuncasApp.config(function(IdleProvider, KeepaliveProvider) {
    alert(3);
    IdleProvider.idle(5);
    IdleProvider.timeout(5);
    KeepaliveProvider.interval(10);
    IdleProvider.interrupt('keydown wheel mousedown touchstart touchmove scroll');
}); 

我从https://jsfiddle.net/esoyke/fkqLqy7c/获取代码。

我收到以下错误:

angular.js:13920 TypeError: Cannot read property 'watch' of undefined at b.$scope.start (loginCtrl.js:141)
at b.$scope.validateUser (loginCtrl.js:18)
at fn (eval at compile (angular.js:14817), <anonymous>:2:227) at b (angular.js:15906)
at e (angular.js:25885)
at b.$eval (angular.js:17682)
at b.$apply (angular.js:17782)
at HTMLButtonElement.<anonymous> (angular.js:25890)
at Sf (angular.js:3497)
at HTMLButtonElement.d (angular.js:3485)
4

1 回答 1

0

Please confirm that you have this dependency properly injected as in var myApp = angular.module("myApp", ['ngIdle']) and in your controller as in app.controller('DemoCtrl', function($scope, Idle /*other injections*/).

于 2019-11-11T21:30:32.067 回答