Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Make all $apply calls safe #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

blittle
Copy link

@blittle blittle commented Sep 19, 2016

Broaden the scope of #170

Generally it is considered safe to do the following:

$timeout(() => {
  $scope.$apply(() => {
     if (!controller.$pristine) {
       controller.$setViewValue('');
     }
  });
}, 0, false);

By using $timeout you are guaranteed to already be in a digest and by passing false as a third parameter $rootScope won't trigger its own digest. The only potential issue is that this will be on another run of the event loop, but given the above code I think it should be fine.

More information: https://docs.angularjs.org/error/$rootScope/inprog?p0=$digest

@blittle blittle force-pushed the fix/digests branch 3 times, most recently from 0b6c80d to 59d63a6 Compare September 19, 2016 18:13
@vanwalj
Copy link

vanwalj commented Oct 26, 2016

This would be great, I sometime encounter this famous $digest already in progress error thrown by ui-mask.

But did you consider using $applyAsync instead of $timeout and $apply?

As far as I know, the only problem with $applyAsync is that it's not supported below Angular 1.3

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants