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.
@gplancke 's watch function may never fire in IE where LastPass doesn't seem to be triggering any event in the browser. I've written a directive that adds a function that runs in intervals, which compares the $viewValue (in ngModel) with the current value of the text field. It doesn't trigger the $setViewValue when the view value is already equal. It's not perfect because it adds timers to the page per field, and takes up more memory. However, it seems to cover all the bases.
app.directive("watchAutofill", [
'$timeout',
function ($timeout) {
var INTERVAL_MS = 500;
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
var timer;
function startTimer() {
timer = $timeout(function () {
var value = element.val();
if (value && ngModel.$viewValue !== value) {
ngModel.$setViewValue(value);
}
startTimer();
}, INTERVAL_MS);
}
scope.$on('$destroy', function () {
$timeout.cancel(timer);
});
startTimer();
}
};
}
]);
@chrisirhc In my app I always have the login form in the DOM but it's not always displayed. So I added two more event listeners to your code to make things easier when the form isn't visible:
scope.$on("loginRequired", startTimer);
scope.$on("loginConfirmed", function () {
$timeout.cancel(timer);
});
Hi, ok, as far as I understand we have two problems here:
- form auto complete by modern browser does not work with angular
- form auto complete by browser extensions or bookmarklets does not work.
Regarding 1.): Modern browser support the input event. This event should be fired whenever the input changes. However, when a browser does an autocomplete for other fields than the current one, it only fires the change event. In modern browsers Angular used to only listen to the input event. Now, it's always listening for the change event. Note: On older browser Angular has always listened for the change event...
Regarding 2.): If those third party libraries change the DOM and don't fire any events like change or input Angular has no way of detecting this. In fact, no other js library is able to do this without doing things like polling, which don't scale for a complex single page web app. If you experience this problem, I would suggest filing a bug for that third party library...
Is there anything I missed here? Feedback welcome!
@all: This just landed in master. Could you double check if we can close this issue now?
@tbosch Could you reference the commit here?
Update Ignore me. I didn't read. :)
I hope this fix will be in 1.2.3 release.
It seems as firefox for android has this issue for input type="time". The ng-model scope is not updated when a time is picked. I've also heard about iPhone 3 with IOS 5 having trouble but that is not yet verified.
I've plunked it on: http://plnkr.co/edit/iO9ClB
The angular-alert should show the time in firefox for android, but shows an empty string instead.
I've just moved to AngularJS 1.2.3 with tbosch commit, but still saved passwords not works for me on Chrome. I also removed ngDisable from submit button - but anyway it sends empty data.
@all: The last commit will be in 1.2.3.
Thanks for testing! Open issue: Autocomplete for login forms when a page loads. This won't make it into 1.2.3 though...
Details: In login forms (with username and password), the browser fills out the fields after the page has loaded. However, the browser does not fire any event for this, and the timing is different between browsers (e.g. on current Chrome it will fire some milliseconds after load event). See e.g. http://stackoverflow.com/questions/11708092/detecting-browser-autofill.
Proposed solution: Wait 100ms after the DOMContentLoaded event and then check all input fields if:
- the model in the scope is empty
- element.value is filled but is different than element.getAttribute('value").
In that case, update the scope with the new values. This should only trigger one $digest only if the browser changed something.
@tbosch Your proposal is same to @alexpirine hack - I'm actually using it right now. But there is another problem - when I first time open my app it works - and I can login with autofilled saved password ( even ngDisabled triggers and enables login button ). But after I logout - nothing happens, but I'm not sure for now why it's happing.
Another issue I've noticed is how FF (At least 25.1 anyways) doesn't fill in the username and password until you start typing the username and select the username you want or tab down into the password field. In this scenario Angular sees the auto completed username but not the auto filled password. Since this happens when the user types it, its not possible to cover this scenario by waiting a few ms after page load.
@mikeobrien Did you test with the new Angular 1.2.3 release from today? When the browser changes something later it should fire a change event, and we are listening for this now.
@sabbaka What do you mean regarding after logout? What should happen and what is not happening?
@tbosch Yeah, 1.2.3. Doesn't appear to be working in that scenario where you tab into the pw field to auto fill it. I couldn't actually get FF to automatically fill out the entire form on page load so I wasn't able to test that scenario. Ended up using @chrisirhc's solution as a stopgap.
@tbosch Login and password inputs should be autofilled again, but after logout they won't get autofilled. So, for example, if I open an app for the first time chrome autofills login's form and I can login ( after using hack ), but if I logout - fields won't autofill.
Tested 1.2.3 in firefox 25.0.1. Doesnt seem to work. I have only one user, so the username & password are autofilled. change event never fires..hmm
+1 not works with typeahead.js , I checked the value of input is correct , but the ng-model's value is incorrect !
Ok, auto complete for fields other than username and password fields should work fine with current browsers. Here is a demo page that works fine in FF and Chrome: http://plnkr.co/edit/lE17EICzvDAVIdvySnY1?p=preview
However, autocomplete for username/password fields is buggy, at least in Chrome:
- during startup: see the bug report from @alexpirine (https://code.google.com/p/chromium/issues/detail?id=307308) earlier in this post.
- during runtime: when entering a username, Chrome is able to auto fill the password field. However, it does not fire the change event in that case.
We discussed this issue internally and decided that we won't implement polling the DOM for changes that the browsers make when filling in username and passwords without firing a JS event.
Please note that this is a problem that all other JS MVC frameworks also share.
Feel free to implement a polling solution when needed or wait for new browser features like requestAutoComplete in Chrome (http://www.chromium.org/developers/using-requestautocomplete).
+1
so how about this:
let's check if dom mutation observers notify us about the autocompletion event and if they do let's do the following
- use dom mutation observers (DMO) to get the changes
- if polyfill for DMO if available
- not support autocomplete if native or polyfill implementation of DMO is not available
we should ensure that:
- we handle cases when a jquery plugin updates dom and triggers the change event (don't digest twice)
- if angular changes the dom, ignore that change too