简体   繁体   中英

ng-hide not working with setInterval

HTML:

<div ng-hide="!timeout">
     First
</div>

<div ng-hide="timeout">
    Second   
</div>

JS:

var counter = 0;
$scope.timeout = false;

var interval = setInterval(function loop() { 
    if (++counter == 4){
                clearInterval(interval);
                $scope.timeout = true;
        }
        return loop
}(), 5000);

After I run, the result shown "First". After counter reached to 4 and $scope.timeout change to true the result still shown "First" instead of "Second".

Thank you.

setInterval is not wrapped in a $scope.$apply() Either, use the angular provided $interval or add $scope.$apply(); in your setInterval function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM