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

Commit d21dff2

Browse files
jbedardcaitp
authored andcommitted
fix(ngmodel): fixing many keys incorrectly marking inputs as dirty
1 parent 55d9db5 commit d21dff2

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/ng/directive/input.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,10 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
973973
}
974974

975975
var listener = function(ev) {
976+
if (timeout) {
977+
$browser.defer.cancel(timeout);
978+
timeout = null;
979+
}
976980
if (composing) return;
977981
var value = element.val(),
978982
event = ev && ev.type;
@@ -999,11 +1003,13 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
9991003
} else {
10001004
var timeout;
10011005

1002-
var deferListener = function(ev) {
1006+
var deferListener = function(ev, input, origValue) {
10031007
if (!timeout) {
10041008
timeout = $browser.defer(function() {
1005-
listener(ev);
10061009
timeout = null;
1010+
if (!input || input.value !== origValue) {
1011+
listener(ev);
1012+
}
10071013
});
10081014
}
10091015
};
@@ -1015,7 +1021,7 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10151021
// command modifiers arrows
10161022
if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40)) return;
10171023

1018-
deferListener(event);
1024+
deferListener(event, this, this.value);
10191025
});
10201026

10211027
// if user modifies input value using context menu in IE, we need "paste" and "cut" events to catch it

test/ng/directive/inputSpec.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -1804,17 +1804,21 @@ describe('input', function() {
18041804
}
18051805
});
18061806

1807-
describe('"paste" and "cut" events', function() {
1807+
describe('"keydown", "paste" and "cut" events', function() {
18081808
beforeEach(function() {
18091809
// Force browser to report a lack of an 'input' event
18101810
$sniffer.hasEvent = function(eventName) {
18111811
return eventName !== 'input';
18121812
};
18131813
});
18141814

1815-
it('should update the model on "paste" event', function() {
1815+
it('should update the model on "paste" event if the input value changes', function() {
18161816
compileInput('<input type="text" ng-model="name" name="alias" ng-change="change()" />');
18171817

1818+
browserTrigger(inputElm, 'keydown');
1819+
$browser.defer.flush();
1820+
expect(inputElm).toBePristine();
1821+
18181822
inputElm.val('mark');
18191823
browserTrigger(inputElm, 'paste');
18201824
$browser.defer.flush();
@@ -1830,6 +1834,21 @@ describe('input', function() {
18301834
expect(scope.name).toEqual('john');
18311835
});
18321836

1837+
it('should cancel the delayed dirty if a change occurs', function() {
1838+
compileInput('<input type="text" ng-model="name" />');
1839+
var ctrl = inputElm.controller('ngModel');
1840+
1841+
browserTrigger(inputElm, 'keydown', {target: inputElm[0]});
1842+
inputElm.val('f');
1843+
browserTrigger(inputElm, 'change');
1844+
expect(inputElm).toBeDirty();
1845+
1846+
ctrl.$setPristine();
1847+
scope.$apply();
1848+
1849+
$browser.defer.flush();
1850+
expect(inputElm).toBePristine();
1851+
});
18331852
});
18341853

18351854

0 commit comments

Comments
 (0)