angualr 之 $$phase

对于angular,

$$phase 是 作为angular 内部状态表示位,用来标示当前是处于哪个阶段。

用有的阶段有

$digest

$apply

在使用的是例如你想调用scope.$apply的时候,经常会遇到这样的错误

Error:$apply already in progress

为了预防这样的错误,

有人是这么写的

if (!scope.$$phase && !scope.$root.$$phase){//脏值检测
    scope.$apply();  
}

一个合理的做法

就是使用$timeout 代替使用,这种写法是目前最好的方法了。

$timeout(function(){//angular本身检测不到异步操作,比如原生的定时器,所以要用angular内部的$timeout
    // anything you want can go here and will safely be run on the next digest. })
原文地址:https://www.cnblogs.com/luckyXcc/p/5957344.html