Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit ff428e7

Browse files
committed
fix(input): don't dirty model when input event triggered due to placeholder change
Certain versions of IE inexplicably trigger an input event in response to a placeholder being set. It is not possible to sniff for this behaviour nicely as the event is not triggered if the element is not attached to the document, and the event triggers asynchronously so it is not possible to accomplish this without deferring DOM compilation and slowing down load times. Closes #2614 Closes #5960
1 parent a990078 commit ff428e7

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/ng/directive/input.js

+10
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ function addNativeHtml5Validators(ctrl, validatorName, element) {
880880

881881
function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
882882
var validity = element.prop('validity');
883+
var placeholder = element[0].placeholder, noevent = {};
883884

884885
// In composition mode, users are still inputing intermediate text buffer,
885886
// hold the listener until composition is done.
@@ -902,6 +903,15 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
902903
var value = element.val(),
903904
event = ev && ev.type;
904905

906+
// IE (11 and under) seem to emit an 'input' event if the placeholder value changes.
907+
// We don't want to dirty the value when this happens, so we abort here. Unfortunately,
908+
// IE also sends input events for other non-input-related things, (such as focusing on a
909+
// form control), so this change is not entirely enough to solve this.
910+
if (msie && (ev || noevent).type === 'input' && element[0].placeholder !== placeholder) {
911+
placeholder = element[0].placeholder;
912+
return;
913+
}
914+
905915
// By default we will trim the value
906916
// If the attribute ng-trim exists we will avoid trimming
907917
// e.g. <input ng-model="foo" ng-trim="false">

test/ng/directive/inputSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,23 @@ describe('input', function() {
520520
}
521521
});
522522

523+
it('should not dirty the model on an input event in response to a placeholder change', inject(function($sniffer) {
524+
if (msie && $sniffer.hasEvent('input')) {
525+
compileInput('<input type="text" ng-model="name" name="name" />');
526+
inputElm.attr('placeholder', 'Test');
527+
browserTrigger(inputElm, 'input');
528+
529+
expect(inputElm.attr('placeholder')).toBe('Test');
530+
expect(inputElm).toBePristine();
531+
532+
inputElm.attr('placeholder', 'Test Again');
533+
browserTrigger(inputElm, 'input');
534+
535+
expect(inputElm.attr('placeholder')).toBe('Test Again');
536+
expect(inputElm).toBePristine();
537+
}
538+
}));
539+
523540
describe('"change" event', function() {
524541
function assertBrowserSupportsChangeEvent(inputEventSupported) {
525542
// Force browser to report a lack of an 'input' event

0 commit comments

Comments
 (0)