ui-mask icon indicating copy to clipboard operation
ui-mask copied to clipboard

Make all $apply calls safe

Open blittle opened this issue 9 years ago • 1 comments

Broaden the scope of https://github.com/angular-ui/ui-mask/pull/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 avatar Sep 19 '16 17:09 blittle

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

vanwalj avatar Oct 26 '16 16:10 vanwalj