Ng-Show 无法正常工作

Ng-Show is not working properly

$scope.stay = function() {
    alert("Inside Keep me In")
    $scope.timed = false;
    $scope.isLogStatus = true;
}

$scope.displayAlert = function() {
    $scope.timed = true;
    alert("inside display")
}

function idleTimer() {
    var t;
    $window.onmousemove = resetTimer; // catches mouse movements
    $window.onmousedown = resetTimer; // catches mouse movements
    $window.onclick = resetTimer; // catches mouse clicks
    $window.onscroll = resetTimer;
    //window.reload=$scope.stay();   // catches scrolling
    // window.onkeypress = resetTimer;  //catches keyboard actions

    function logout() {
        //Adapt to actual logout script
        $scope.displayAlert();
        alert('insinde logout');
        //  delete $window.localStorage.user;
        //  location.href="/";
    }

    function reload() {
        $window.location = self.location.href; //Reloads the current page
    }

    function resetTimer() {
        alert("timer reset")
        clearTimeout(t);
        // t = setTimeout(logout, 600000);  // time is in milliseconds (1000 is 1 second)
        $timeout(function() {
            alert("timout triggered");
            $scope.displayAlert();
        }, 9000); // time is in milliseconds (1000 is 1 second)
    }
}
idleTimer();

我在 html 以上使用,默认情况下,如果我保留

$scope.timed=true;

有效 当我点击登录时,我正在做

 $scope.timed=false;

如果时间超过 10 分钟,我会再做一次

$scope.timed=true;

(不触发 ng-show) 然后显示不工作

这是控制器正在发生的事情

$scope.stay = function() {
    alert("Inside Keep me In")
    $scope.timed = false;
    $scope.isLogStatus = true;
}

$scope.displayAlert = function() {
    $scope.timed = true;
    alert("inside display")

}

function idleTimer() {
    var t;

    window.onmousemove = resetTimer; // catches mouse movements
    window.onmousedown = resetTimer; // catches mouse movements
    window.onclick = resetTimer; // catches mouse clicks
    window.onscroll = resetTimer;
    //window.reload=$scope.stay();   // catches scrolling
    // window.onkeypress = resetTimer;  //catches keyboard actions

    function logout() {
        //Adapt to actual logout script
        $scope.displayAlert();
        alert('insinde logout');
        //  delete $window.localStorage.user;
        //  location.href="/";
    }

    function reload() {
        window.location = self.location.href; //Reloads the current page
    }

    function resetTimer() {
        // alert("timer reset")
        clearTimeout(t);
        // t = setTimeout(logout, 600000);  // time is in milliseconds (1000 is 1 second)
        t = setTimeout(logout, 9000); // time is in milliseconds (1000 is 1 second)
    }
}
idleTimer();
// Get the topbar menu
$scope.menu = Menus.getMenu('topbar');

问题在于您调用 logout 函数的方式。您是通过 angular 框架之外调用它的,这意味着 angular 不会 运行 摘要循环 因此 [=25= 中的任何更改]scope 不会反映到 view.

为了验证,您可以使用 $timeout 将代码包装在注销函数中。请不要忘记将其添加到 dependencies.

$timeout(function(){
   $scope.displayAlert();
});

理想情况下,您应该使用 angular wrappers 例如$window 对应 window$timeout 对应 setTimeout。通过这样做 angular,自动观察变化并相应地 运行 消化周期。