ui-mask
ui-mask copied to clipboard
Make all $apply calls safe
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
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