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

Commit 2ee9eed

Browse files
committed
fix(input): update $viewValue when cleared
- Fix when user clicks clear button in an input element in IE, $viewValue not being correctly updated
1 parent 9686f3a commit 2ee9eed

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/ng/directive/input.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ var WEEK_REGEXP = /^(\d{4,})-W(\d\d)$/;
3434
var MONTH_REGEXP = /^(\d{4,})-(\d\d)$/;
3535
var TIME_REGEXP = /^(\d\d):(\d\d)(?::(\d\d)(\.\d{1,3})?)?$/;
3636

37+
var IE_INPUTS_WITH_CLEARING = ['text', 'number', 'date', 'datetime-local', 'email', 'month', 'time', 'url', 'week'];
38+
3739
var PARTIAL_VALIDATION_EVENTS = 'keydown wheel mousedown';
3840
var PARTIAL_VALIDATION_TYPES = createMap();
3941
forEach('date,datetime-local,month,time,week'.split(','), function(type) {
@@ -1126,7 +1128,11 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
11261128
});
11271129
}
11281130

1129-
var timeout;
1131+
var timeout, oldVal;
1132+
var viewValueUpdated = true, msieInput = msie >= 10 && msie <= 11;
1133+
if (msieInput && attr.type in IE_INPUTS_WITH_CLEARING) {
1134+
oldVal = element.val();
1135+
}
11301136

11311137
var listener = function(ev) {
11321138
if (timeout) {
@@ -1152,10 +1158,18 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
11521158
}
11531159
};
11541160

1161+
function ieListener(ev) {
1162+
var val = element.val();
1163+
if (val === oldVal && !viewValueUpdated) return;
1164+
oldVal = val;
1165+
viewValueUpdated = false;
1166+
listener(ev);
1167+
}
1168+
11551169
// if the browser does support "input" event, we are fine - except on IE9 which doesn't fire the
11561170
// input event on backspace, delete or cut
11571171
if ($sniffer.hasEvent('input')) {
1158-
element.on('input', listener);
1172+
element.on('input', msie ? ieListener : listener);
11591173
} else {
11601174
var deferListener = function(ev, input, origValue) {
11611175
if (!timeout) {
@@ -1212,6 +1226,10 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12121226
// Workaround for Firefox validation #12102.
12131227
var value = ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue;
12141228
if (element.val() !== value) {
1229+
// Workaround for IE 10 & 11 input updates #11193
1230+
if (msieInput) {
1231+
viewValueUpdated = true;
1232+
}
12151233
element.val(value);
12161234
}
12171235
};

src/ng/sniffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function $SnifferProvider() {
7373
// when cut operation is performed.
7474
// IE10+ implements 'input' event but it erroneously fires under various situations,
7575
// e.g. when placeholder changes, or a form is focused.
76-
if (event === 'input' && msie <= 11) return false;
76+
if (event === 'input' && msie <= 9) return false;
7777

7878
if (isUndefined(eventSupport[event])) {
7979
var divElm = document.createElement('div');

test/ng/snifferSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe('$sniffer', function() {
135135
// IE10+ implementation is fubared when mixed with placeholders
136136
mockDivElement = {oninput: noop};
137137

138-
expect($sniffer.hasEvent('input')).toBe(!(msie && msie <= 11));
138+
expect($sniffer.hasEvent('input')).toBe(!(msie && msie <= 9));
139139
});
140140
});
141141

0 commit comments

Comments
 (0)