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

Commit

Permalink
fix(input): update $viewValue when cleared
Browse files Browse the repository at this point in the history
- 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
1 parent 9686f3a commit 1da5563
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/ng/directive/input.js
Expand Up @@ -1103,11 +1103,11 @@ function stringBasedInputType(ctrl) {
}

function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
baseInputType(scope, element, attr, ctrl, $sniffer, $browser, 'text');
stringBasedInputType(ctrl);
}

function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
function baseInputType(scope, element, attr, ctrl, $sniffer, $browser, type) {
var type = lowercase(element[0].type);

// In composition mode, users are still inputing intermediate text buffer,
Expand All @@ -1126,7 +1126,10 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
});
}

var timeout;
var timeout, oldVal;
if (type === 'text') {
oldVal = element.val();
}

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

function ieListener(ev) {
var val = element.val();
if (val === oldVal) return;
oldVal = val;
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
2 changes: 1 addition & 1 deletion src/ng/sniffer.js
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
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 comments on commit 1da5563

Please sign in to comment.