search

Home  >  Q&A  >  body text

angular.js - What is $root.$$phase in angularjs?

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~~~

伊谢尔伦伊谢尔伦2826 days ago693

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