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

Commit

Permalink
fix(input): Support form auto complete on modern browser
Browse files Browse the repository at this point in the history
Although modern browser support the "input" event, they still only fire
the "change" event when they auto complete form elements
other than the currently selected one.

Related to #1460
  • Loading branch information
tbosch committed Nov 23, 2013
1 parent 84e0eea commit a090400
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,15 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
deferListener();
});

// if user paste into input using mouse, we need "change" event to catch it
element.on('change', listener);

// if user modifies input value using context menu in IE, we need "paste" and "cut" events to catch it
if ($sniffer.hasEvent('paste')) {
element.on('paste cut', deferListener);
}
}

// if user paste into input using mouse on older browser
// or form autocomplete on newer browser, we need "change" event to catch it
element.on('change', listener);

ctrl.$render = function() {
element.val(ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue);
Expand Down
26 changes: 26 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,32 @@ describe('input', function() {
expect(scope.name).toEqual('adam');
});

describe('"change" event', function() {
function assertBrowserSupportsChangeEvent(inputEventSupported) {
// Force browser to report a lack of an 'input' event
$sniffer.hasEvent = function(eventName) {
if (eventName === 'input' && !inputEventSupported) {
return false;
}
return true;
};
compileInput('<input type="text" ng-model="name" name="alias" />');

inputElm.val('mark');
browserTrigger(inputElm, 'change');
expect(scope.name).toEqual('mark');
}

it('should update the model event if the browser does not support the "input" event',function() {
assertBrowserSupportsChangeEvent(false);
});

it('should update the model event if the browser supports the "input" ' +
'event so that form auto complete works',function() {
assertBrowserSupportsChangeEvent(true);
});
});

describe('"paste" and "cut" events', function() {
beforeEach(function() {
// Force browser to report a lack of an 'input' event
Expand Down

2 comments on commit a090400

@petebacondarwin
Copy link
Contributor

Choose a reason for hiding this comment

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

@tbosch - no capital letter at the start of the commit message subject

@tbosch
Copy link
Contributor Author

@tbosch tbosch commented on a090400 Nov 27, 2013

Choose a reason for hiding this comment

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

@petebacondarwin Thanks for the hint. Will make it better next time...

Please sign in to comment.