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;。
谢谢。
答案 0 :(得分:2)
setInterval未包含在$scope.$apply()
中
或者,在setInterval函数中使用提供的角度$interval
或添加$scope.$apply();
。