Skip to content

Commit

Permalink
fix(input): prevent double $digest when using jQuery trigger.
Browse files Browse the repository at this point in the history
If an event was performed natively, jQuery sets the isTrigger property.
When triggering event manually, the field is not present. Manually
triggered events are performed synchronously which causes the "$digest
already in progress" error.

Closes angular#5293
  • Loading branch information
mgol authored and jamesdaily committed Jan 27, 2014
1 parent adad337 commit 9aa24fd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
});
}

var listener = function() {
var listener = function(ev) {
if (composing) return;
var value = element.val();

Expand All @@ -419,9 +419,17 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
}

if (ctrl.$viewValue !== value) {
scope.$apply(function() {
// If an event was performed natively, jQuery sets the isTrigger property.
// When triggering event manually, the field is not present. Manually
// triggered events are performed synchronously which causes the "$digest
// already in progress" error.
if (ev && ev.isTrigger) {
ctrl.$setViewValue(value);
});
} else {
scope.$apply(function() {
ctrl.$setViewValue(value);
});
}
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/ngScenario/Scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ function callerFile(offset) {
* To work around this we instead use our own handler that fires a real event.
*/
(function(fn){
var parentTrigger = fn.trigger;
// We need a handle to the original trigger function for input tests.
var parentTrigger = fn._originalTrigger = fn.trigger;
fn.trigger = function(type) {
if (/(click|change|keydown|blur|input|mousedown|mouseup)/.test(type)) {
var processDefaults = [];
Expand Down
17 changes: 17 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,23 @@ describe('input', function() {
'event so that form auto complete works',function() {
assertBrowserSupportsChangeEvent(true);
});

if (!_jqLiteMode) {
it('should not cause the double $digest when triggering an event using jQuery', function() {
$sniffer.hasEvent = function(eventName) {
return eventName !== 'input';
};

compileInput('<input type="text" ng-model="name" name="alias" ng-change="change()" />');

scope.field = 'fake field';
scope.$watch('field', function() {
// We need to use _originalTrigger since trigger is modified by Angular Scenario.
inputElm._originalTrigger('change');
});
scope.$apply();
});
}
});

describe('"paste" and "cut" events', function() {
Expand Down

0 comments on commit 9aa24fd

Please sign in to comment.