您的位置:首页 > Web前端 > AngularJS

angualr 之 $$phase

2015-11-23 00:00 676 查看
摘要: 正对于 错误 (Error:$apply already in progres) 的完美解决方案

对于angular,

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

用有的阶段有

$digest

$apply

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

Error:$apply already in progress

为了预防这样的错误,

有人是这么写的

if (!scope.$$phase && !scope.$root.$$phase){
scope.$apply();
}

一个合理的做法

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

$timeout(function(){
// anything you want can go here and will safely be run on the next digest.
})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  angular