search

Home  >  Q&A  >  body text

angular.js - angularjs 中的$root.$$phase是什么东西?

有如下一段ng代码:

$scope.safeApply = function(fn){
   var phase = this.$root.$$phase;
   if (phase == '$apply' || phase == '$digest') {
       if (fn && ( typeof (fn) === 'function')) {
          fn();
       }
   } else {
       this.$apply(fn);
   }
}

这段代码的意思应该是说,执行fn这个函数,是否需要调用scope.$apply(),如果不需要就直接调用fn,如果需要,那就调用scope.$apply(fn)...
但是,我想知道$root.$$phase是什么,为什么通过它就可以判断是否需要执行$apply.希望能有懂ng原理的高手帮忙解答一下~ 谢谢~~~

伊谢尔伦伊谢尔伦2743 days ago655

reply all(1)I'll reply

  • 世界只因有你

    世界只因有你2017-05-15 16:51:29

    $$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.

    reply
    0
  • Cancelreply