【问题标题】:Ng-Show is not working properlyNg-Show 无法正常工作
【发布时间】:2018-07-20 11:10:22
【问题描述】:
$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');

【问题讨论】:

  • 请添加调用切换timed的代码
  • 好的,我已经添加了,请检查。 @NikhilAggarwal
  • 尝试使用$timeout 而不是setTimeout
  • 实际上超时工作,警报也正在显示,但 scope.timed 没有显示@AlekseySolovey

标签: html css angularjs mean-stack


【解决方案1】:

问题在于您调用logout 函数的方式。您通过 Angular 框架外部调用它,这意味着 Angular 不会运行 digest cycle,因此 scope 的任何更改都不会反映到 view。

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

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

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

【讨论】:

  • @srikanthnaidu - 你能把这个问题复制到我可以调试的地方吗?
  • function resetTimer() { // alert("timer reset") clearTimeout(t); // t = setTimeout(注销, 600000); // 时间以毫秒为单位(1000 是 1 秒) t= $timeout(function(){ $scope.displayAlert(); }, 9000); // 时间以毫秒为单位(1000 为 1 秒) }
  • @srikanthnaidu - 你是否也将window更改为$window
  • 是的,我做到了
  • Nikhil,我发现了错误,它来自我这边,在我修复它之后,你的解决方案对我有用,谢谢
猜你喜欢
  • 2014-04-27
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 1970-01-01
  • 2016-04-24
相关资源
最近更新 更多