ng-hide不使用setInterval

时间:2015-11-30 08:58:23

标签: javascript angularjs ionic-framework setinterval ng-hide

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);

跑完后,结果显示&#34; First&#34;。在计数器达到4并且$ scope.timeout变为true后,结果仍然显示&#34; First&#34;而不是&#34;第二&#34;。

谢谢。

1 个答案:

答案 0 :(得分:2)

setInterval未包含在$scope.$apply()中 或者,在setInterval函数中使用提供的角度$interval或添加$scope.$apply();