8000 fix(input): update $viewValue when cleared by wesleycho · Pull Request #14772 · angular/angular.js · GitHub
[go: up one dir, main page]

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

fix(input): update $viewValue when cleared #14772

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(input): update $viewValue when cleared
- Fix when user clicks clear button in an input element in IE, $viewValue not being correctly updated
  • Loading branch information
wesleycho committed Jun 14, 2016
commit 2feeed68ef8e0b9d7b0605a59630e0c0df56206d
20 changes: 18 additions & 2 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,11 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
});
}

var timeout;
var timeout, oldVal;
var viewValueUpdated = false, msieInput = msie >= 10 && msie <= 11;
if (msieInput) {
oldVal = element.val();
}

var listener = function(ev) {
if (timeout) {
Expand All @@ -1152,10 +1156,18 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
}
};

function ieListener(ev) {
var val = element.val();
if (val === oldVal && !viewValueUpdated) return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that the validity changed but the value did not (similar to the value === '' && ctrl.$$hasNativeValidators check in listener)? Although I'm not seeing how that would have been handled previously either (in the !hasEvent('input') case)...

oldVal = val;
viewValueUpdated = false;
listener(ev);
}

// if the browser does support "input" event, we are fine - except on IE9 which doesn't fire the
// input event on backspace, delete or cut
if ($sniffer.hasEvent('input')) {
element.on('input', listener);
element.on('input', msie ? ieListener : listener);
} else {
var deferListener = function(ev, input, origValue) {
if (!timeout) {
Expand Down Expand Up @@ -1212,6 +1224,10 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
// Workaround for Firefox validation #12102.
var value = ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue;
if (element.val() !== value) {
// Workaround for IE 10 & 11 input updates #11193
if (msieInput) {
viewValueUpdated = true;
}
element.val(value);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/ng/sniffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function $SnifferProvider() {
// when cut operation is performed.
// IE10+ implements 'input' event but it erroneously fires under various situations,
// e.g. when placeholder changes, or a form is focused.
if (event === 'input' && msie <= 11) return false;
if (event === 'input' && msie <= 9) return false;

if (isUndefined(eventSupport[event])) {
var divElm = document.createElement('div');
Expand Down
2 changes: 1 addition & 1 deletion test/ng/snifferSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('$sniffer', function() {
// IE10+ implementation is fubared when mixed with placeholders
mockDivElement = {oninput: noop};

expect($sniffer.hasEvent('input')).toBe(!(msie && msie <= 11));
expect($sniffer.hasEvent('input')).toBe(!(msie && msie <= 9));
});
});

Expand Down
0