What is $$phase in AngularJS?

angularjs

Solution

`$$phase` is a flag set while angular is in a `$digest` cycle.

Sometimes (in rare cases), you want to check `$$phase` on the scope before doing an `$apply`. An error occurs if you try to `$apply` during a `$digest`:

Error: $apply already in progress

Problem

I found this code snippet which is part of a angular directive someone wrote for bootstrap modal. ``` //Update the visible value when the dialog is closed //through UI actions (Ok, cancel, etc.) element.bind("hide.bs.modal", function () { scope.modalVisible = false; if (!scope.$$phase && !scope.$root.$$phase) scope.$apply(); }); ``` I understood that this part is for the latter half of two way binding we bind to hide.bs.modal event and update modal when UI changes. I just wanted to know why is the person checking $$phase for scope and rootScope before calling apply ? Can't we straightaway call apply ? What is $$phase here? I tried searching a lot, couldn't find any good explanation. EDIT: I found where I saw the example: Simple Angular Directive for Bootstrap Modal

Original source

Related problems