Skip to content

Commit fab6761

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 angular#2614 Closes angular#5960
1 parent e020916 commit fab6761

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/ng/directive/input.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ function validate(ctrl, validatorName, validity, value){
405405
}
406406

407407
function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
408+
var placeholder = element[0].placeholder, noevent = {};
408409
// In composition mode, users are still inputing intermediate text buffer,
409410
// hold the listener until composition is done.
410411
// More about composition events: https://developer.mozilla.org/en-US/docs/Web/API/CompositionEvent
@@ -420,10 +421,18 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
420421
});
421422
}
422423

423-
var listener = function() {
424+
var listener = function(event) {
424425
if (composing) return;
425426
var value = element.val();
426427

428+
// Some versions of MSIE emit an 'input' event when the placeholder attribute/property
429+
// change. This hack prevents an otherwise pristine field from being dirtied on IE
430+
// browsers.
431+
if (msie && (event || noevent).type === 'input' && element[0].placeholder !== placeholder) {
432+
placeholder = element[0].placeholder;
433+
return;
434+
}
435+
427436
// By default we will trim the value
428437
// If the attribute ng-trim exists we will avoid trimming
429438
// e.g. <input ng-model="foo" ng-trim="false">

test/ng/directive/inputSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,28 @@ describe('input', function() {
509509
});
510510
}
511511

512+
513+
it('should not dirty the model on an input event in response to a placeholder change', inject(function($sniffer) {
514+
if (msie && $sniffer.hasEvent('input')) {
515+
compileInput('<input type="text" ng-model="name" name="name" />');
516+
517+
expect(inputElm).toBePristine();
518+
519+
inputElm.attr('placeholder', 'Test');
520+
browserTrigger(inputElm, 'input');
521+
522+
expect(inputElm.attr('placeholder')).toBe('Test');
523+
expect(inputElm).toBePristine();
524+
525+
inputElm.attr('placeholder', 'Test Again');
526+
browserTrigger(inputElm, 'input');
527+
528+
expect(inputElm.attr('placeholder')).toBe('Test Again');
529+
expect(inputElm).toBePristine();
530+
}
531+
}));
532+
533+
512534
describe('"change" event', function() {
513535
function assertBrowserSupportsChangeEvent(inputEventSupported) {
514536
// Force browser to report a lack of an 'input' event

0 commit comments

Comments
 (0)