Form model doesn't update on autocomplete
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:
- Enter data into the form a few times so that auto-complete is activated.
- Then enter a few characters that triggers an auto-complete.
- Hit return.
- 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.
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.
+1
+1
+1
+1
+1
:+1:
+1
@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)
+2
Google Maps JavaScript API v3, Place Autocomplete, has this problem as well.
+1
+4 on behalf of our dev team
+1
+1
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 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.
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:
- are we overwriting a model value that the developer actually wants us to use
- 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
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.
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 :(