$apply vs $digest
- $apply会使ng进入
$digest cycle
,并从$rootScope开始遍历(深度优先)检查数据变更。 - $digest仅会检查该scope和它的子scope,当你确定当前操作仅影响它们时,用$digest可以稍微提升性能。
参考《mastering web application development with angularjs》 P308
延迟执行
- 一些不必要的操作,放到
$timeout
里面延迟执行。 - 如果不涉及数据变更,还可以加上第三个参数false,避免调用
$apply
。 - 对时间有要求的,第二个参数可以设置为0。
$scope.name = data.name;
$timeout( (){
//do sth later,such as log
},153)!important; border:0px!important; font-size:1em!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-weight:bold!important; min-height:auto!important; background:none!important">false );
});