angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Form model doesn't update on autocomplete

Open msgilligan opened this issue 14 years ago • 198 comments

I'm having problems with Safari 6.0 auto-complete on some simple forms on Angular 1.0.2.

When Safari uses auto-complete to fill in the form, Angular seems to be unaware of the new characters that were entered by auto-complete.
Please see the attached image for an example.

To reproduce:

  1. Enter data into the form a few times so that auto-complete is activated.
  2. Then enter a few characters that triggers an auto-complete.
  3. Hit return.
  4. Compare the alert message with the contents of the input field.

I've created a jsFiddle that can be used to reproduce the problem: http://jsfiddle.net/msgilligan/5cynT/

I've seen a similar problem when Safari 6.0 or Chrome 22.0.1229.79 automatically fills in a password field that has a 'required' attribute. The form is not marked as valid and I have to go enter a space after the password and then delete it.

msgilligan avatar Oct 15 '12 17:10 msgilligan

here is a hacky little directive I made that addresses both the chrome/safari onload autofill and the firefox/ie onclick/tab variation.

It fires way too many events, but could probably be cleaned up into a workable directive.

nelsonpecora avatar Dec 13 '13 17:12 nelsonpecora

+1

gunta avatar Dec 16 '13 07:12 gunta

+1

rnofenko avatar Dec 16 '13 21:12 rnofenko

+1

timbz avatar Dec 17 '13 08:12 timbz

+1

marcalj avatar Dec 17 '13 08:12 marcalj

+1

GabrielDelepine avatar Dec 17 '13 09:12 GabrielDelepine

:+1:

neilhem avatar Dec 17 '13 09:12 neilhem

+1

tieungaosp avatar Dec 18 '13 03:12 tieungaosp

@IgorMinar: Tried mutation observers, but they don't fire when username/password is auto filled by chrome when the page is loaded (see this fiddle http://jsfiddle.net/QN2vL/).

Reason: Mutation Observers are limited to observing the DOM serialized state, and node.value does not belong to this, see SO answer and chrome issue (marked as won't fix)

tbosch avatar Dec 18 '13 04:12 tbosch

+2

rivera-ernesto avatar Dec 20 '13 02:12 rivera-ernesto

Google Maps JavaScript API v3, Place Autocomplete, has this problem as well.

MarcMouallem avatar Dec 22 '13 10:12 MarcMouallem

+1

sagish avatar Dec 23 '13 09:12 sagish

+4 on behalf of our dev team

cbier avatar Dec 23 '13 17:12 cbier

+1

SridharSubramaniam avatar Dec 29 '13 19:12 SridharSubramaniam

+1

jacqueslareau avatar Dec 30 '13 14:12 jacqueslareau

I'm wondering if we could get a list of browsers which have issues with this, and under what circumstances?

I've just done some investigating during meeting and it seems that Chromium at the very least should emit change on autofill, so that seems like we should be covered there (but please let me know if we're not, it may happen before the input directive is set up, and we might not be initializing the model value from the pre-existing view value, probably for good reason)

I'm not sure how this would affect other browsers, but there's a good chance gecko is working similar to chromium/blink. Test cases so we can figure out exactly what the current issues are would be awesome, it would help a lot to find an effective fix.

caitp avatar Dec 30 '13 20:12 caitp

@caitp I can confirm that this is an issue on google chrome v31.0.1650.63, so I don't think chromium is fully covered here.

joshkurz avatar Jan 02 '14 16:01 joshkurz

I mean under what circumstances? Is the form created / change event fired before angular bootstrap?

If we're getting the change event before bootstrap, we don't assign the value of the field to the model. We could do this as like a one-time thing, but there are some issues with it:

  1. are we overwriting a model value that the developer actually wants us to use
  2. did the pre-existing form value come from an autofill, or from somewhere else?

in angular, the model is meant to drive the view, not vice versa, so it's a bit awkward to fit this in. But since people want it, of course it needs to happen somehow, it would just not be good if it results in hurt performance or brokenness

caitp avatar Jan 02 '14 17:01 caitp

The input's created/changed event is fired after angular bootstrap, but I don't believe a "change" event is being fired at all.

Since we are trying to set the value in the link function the app has long passed bootstrapping, but no value exists in the element yet. This means the "autocomplete event" or "unicorn event" has not fired yet.

I have come to this conclusion by using this directive.

.directive('autocomplete', function() {
    return {
      require: '^ngModel',
      link: function(scope,element,attrs,ngModelCtrl){
        ngModelCtrl.$setViewValue(element.val());
      }
    }
});

To fix this I am just using jQuery and bypassing the model all together for logins. Kinda frowned on, but Id rather do that than use setTimeout or poll for the value.

joshkurz avatar Jan 02 '14 19:01 joshkurz

That's a much better solution than forcing ngModel to do it by default, because we don't know if you want autocomplete/autofill to be affect the model.

Anyways, in Chromium, a change event is fired on autofill --- however there is a subtle distinction between autofill and form autocomplete, which is why it's hard to really picture what's happening. There have also been a lot of inter-browser differences in this area in the past, and I'm not sure how that is looking now, but some browsers will decide not to trigger the 'change' event depending on for instance, field names. And the question of when the events are fired is also a matter which varies from browser to browser.

So it's really a hard problem to solve effectively, and providing a custom directive to tell ngModel to update the model value on load is probably the best thing you can do for it. However there would still be cases where UI changes wouldn't affect the model value unfortunately :( welcome to the web platform, where big is small, round is square, red is green, fish fly and birds swim. It' s very hard to come up with a sensible solution for all browsers :(

caitp avatar Jan 02 '14 19:01 caitp