在angularJS中使用自定义指令ng-idle的问题

时间:2015-06-22 17:14:39

标签: javascript angularjs ng-idle

看来,在angularJS中允许空闲超时类型处理的唯一方法是使用HackedByChinese编写的这个自定义指令ng-idle。我使用这个指令时遇到的问题是,当我在超时后签署用户时,没有任何反应。

我正在使用ng-show做一些基本的事情只是为了感受一下事情,并且只要检测到用户何时空闲,它就可以正常工作,但是一旦用户达到超时状态,没有任何反应。

这是我的JS:

// include the `ngIdle` module
var app = angular.module('demo', ['ngIdle', 'ui.bootstrap']);

app
.controller('EventsCtrl', function($scope, Idle, $modal) {
    $scope.logIn = function(){
        $scope.loggedIn = true;
        Idle.watch();
    };

    $scope.events = [];

    $scope.$on('IdleStart', function() {
        closeModals();   
        $scope.amIdle = true;
    });

    $scope.$on('IdleWarn', function(e, countdown) {
        // follows after the IdleStart event, but includes a countdown until the user is considered timed out
        // the countdown arg is the number of seconds remaining until then.
        // you can change the title or display a warning dialog from here.
        // you can let them resume their session by calling Idle.watch()
    });

    $scope.$on('IdleTimeout', function() {
        // the user has timed out (meaning idleDuration + timeout has passed without any activity)
        // this is where you'd log them
        $scope.loggedIn = false;
        $scope.amIdle = false;
    });

    $scope.$on('IdleEnd', function() {
        // the user has come back from AFK and is doing stuff. if you are warning them, you can use this to hide the dialog
        $scope.amIdle = false;
    });

   /*$scope.$on('Keepalive', function() {
        $scope.amIdle = false;
    });*/

})
.config(function(IdleProvider, KeepaliveProvider) {
    // configure Idle settings
    IdleProvider.idle(5); // in seconds
    IdleProvider.timeout(5); // in seconds
    KeepaliveProvider.interval(1); // in seconds
})

我的HTML:

<!DOCTYPE html>
<html lang="en-us" ng-app="demo">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="angular-idle.js"></script>
<script src="app.js"></script>
<script src="ui-bootstrap-tpls-0.13.0.min.js"></script>

<body ng-controller="EventsCtrl">
    <div ng-show="!loggedIn">
        <label>Log In:<input type="text" name = "user">
            <button type = "submit" ng-click="logIn()">Submit</button>
        </label>
    </div>

    <div ng-show="loggedIn"><h2>NG-Idle test</h2>
        <center><div ng-show="amIdle">Yo wake up</div>
    </div>  
</body>
</html>

1 个答案:

答案 0 :(得分:0)

看来问题是,无论出于何种原因,在更改变量后应用程序都没有更新。这是通过添加

来解决的
$scope.$apply();

在IdleTimeout函数中将两个布尔值设置为false后。