Skip to content

Commit

Permalink
Ugly hack for not overwriting remembered values in Firefox.
Browse files Browse the repository at this point in the history
Note: this commit is not meant for submission to Angular, as it's
missing tests, is probably the wrong approach, etc.--it just works
for us. If anyone wants to clean it up for submission, please do.

For background see:

angular#1460

Also note: this only fixes Firefox, since it auto-fills remembered values
before onload runs. For Chrome, see:

https://code.google.com/p/chromium/issues/detail?id=224405
  • Loading branch information
Stephen Haberman committed Apr 2, 2013
1 parent 85c31e0 commit f6295cc
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/ng/directive/input.js
Expand Up @@ -389,6 +389,20 @@ function isEmpty(value) {

function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {

// Stash the existing value for auto-fill. We do this here instead of the
// NgModelController because .val behaves differently for each control
// (e.g. checkboxes).
//
// Background: Firefox will auto-fill remembered username/passwords before
// body onload runs, if we do not grab the value here, NgModelController
// will later write over the remembered values with whatever is in the model.
//
// Note that Chrome does not auto-fill remembered values before onload
// runs, see: https://code.google.com/p/chromium/issues/detail?id=224405
if (element.val() != "") {
ctrl.$existingValue = element.val();
}

var listener = function() {
var value = element.val();

Expand Down Expand Up @@ -1046,6 +1060,12 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
$scope.$watch(function ngModelWatch() {
var value = ngModelGet($scope);

// if the form was auto-filled and our model has no value, use the form value
if (isNaN(ctrl.$modelValue) && value === undefined && ctrl.$existingValue !== undefined) {
ctrl.$setViewValue(ctrl.$existingValue);
return;
}

// if scope model value and ngModel value are out of sync
if (ctrl.$modelValue !== value) {

Expand Down

0 comments on commit f6295cc

Please sign in to comment.