您的位置:首页 > 移动开发

angular 之 $scope的 $apply $eval $digest

2016-01-21 00:00 501 查看
$apply:function(expr){
try{
beginPhase('$apply');
try{
return this.$eval(expr);
}
finally{
clearPhase();
}
}
catch(e){
$exceptionHander(e);
}
finally{
try{
$rootScope.$digest();
}
catch(e){
$exceptionHander(e);
throw e;
}
}
}

对于 $apply 其实 内部还是调用了 $eval() $rootScope.$digest()

$eval:function(expr,locals){
// 调用了 $phase 服务
return $phase(expr)(this,locals);
}

对于digest 方法

官方介绍

processes all of the watchers of the current scope and its children, because a watcher's listener

can change the model,the digest keeps calling the watchers until no more listeners are firing.

this means that it is possible to get into an infinite loop , the function will throw 'maximum

iteration limit exceeded' if the number of iteration exceeds 10;

usually you don't call digest() directly in controllers or in directive .

instaad, you should call $apply ;

大概解释
就是 $digest 是一个watchers 的大集合,用于监听watcher 的内容变化,然后做脏值检查的。 不推荐直接使用$digest 方法,而是调用 $apply .


// 脏值检查
$digest:function(){
// 实现比较复杂

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: