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

Commit 0b6c80d

Browse files
committed
Make $apply calls safe
1 parent d84a811 commit 0b6c80d

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/mask.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ angular.module('ui.mask', [])
4949
return tempOptions;
5050
}];
5151
})
52-
.directive('uiMask', ['uiMask.Config', function(maskConfig) {
52+
.directive('uiMask', ['uiMask.Config', '$timeout', function(maskConfig, $timeout) {
5353
function isFocused (elem) {
5454
return elem === document.activeElement && (!document.hasFocus || document.hasFocus()) && !!(elem.type || elem.href || ~elem.tabIndex);
5555
}
@@ -444,12 +444,14 @@ angular.module('ui.mask', [])
444444
if (!isValid || value.length === 0) {
445445
valueMasked = '';
446446
iElement.val('');
447-
scope.$apply(function() {
448-
//only $setViewValue when not $pristine to avoid changing $pristine state.
449-
if (!controller.$pristine) {
450-
controller.$setViewValue('');
451-
}
452-
});
447+
$timeout(function() {
448+
scope.$apply(function() {
449+
//only $setViewValue when not $pristine to avoid changing $pristine state.
450+
if (!controller.$pristine) {
451+
controller.$setViewValue('');
452+
}
453+
});
454+
}, 0, false);
453455
}
454456
}
455457
//Check for different value and trigger change.
@@ -597,9 +599,11 @@ angular.module('ui.mask', [])
597599
iElement.val(maskPlaceholder);
598600
// This shouldn't be needed but for some reason after aggressive backspacing the controller $viewValue is incorrect.
599601
// This keeps the $viewValue updated and correct.
600-
scope.$apply(function () {
601-
controller.$setViewValue(''); // $setViewValue should be run in angular context, otherwise the changes will be invisible to angular and user code.
602-
});
602+
$timeout(function() {
603+
scope.$apply(function () {
604+
controller.$setViewValue(''); // $setViewValue should be run in angular context, otherwise the changes will be invisible to angular and user code.
605+
});
606+
}, 0, false);
603607
setCaretPosition(this, caretPosOld);
604608
return;
605609
}
@@ -640,9 +644,11 @@ angular.module('ui.mask', [])
640644
//we need this check. What could happen if you don't have it is that you'll set the model value without the user
641645
//actually doing anything. Meaning, things like pristine and touched will be set.
642646
if (valAltered) {
643-
scope.$apply(function () {
644-
controller.$setViewValue(valMasked); // $setViewValue should be run in angular context, otherwise the changes will be invisible to angular and user code.
645-
});
647+
$timeout(function() {
648+
scope.$apply(function () {
649+
controller.$setViewValue(valMasked); // $setViewValue should be run in angular context, otherwise the changes will be invisible to angular and user code.
650+
});
651+
}, 0, false);
646652
}
647653

648654
// Caret Repositioning

0 commit comments

Comments
 (0)