There is the following ng code:
$scope.safeApply = function(fn){
var phase = this.$root.$$phase;
if (phase == '$apply' || phase == '$digest') {
if (fn && ( typeof (fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
}
The meaning of this code should be to say whether it is necessary to call scope.$apply() to execute the function fn. If not, call fn directly. If necessary, call scope.$apply(fn).. .
However, I want to know what $root.$$phase is and why it can be used to determine whether $apply needs to be executed. I hope someone who understands the ng principle can help answer it~ Thank you~~~
$$phase
is a status flag used internally by angluar to identify whether it is currently in the digest state.Usually there is no need to care about this status in the program (two
$
打头更说明它是内部使用的),如果你写出了需要关心这个状态的代码,比如这个safeApply
, then there must be something wrong.