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.
@jamm I need it on a page where the user can input an arbitrary amount of addresses and thus can summon a theoretically infinite amount of address forms (which are actual directives themselves). With four such forms on screen and 6 autofillables in each, my CPU spiked out. I'm sure it'll be fine for some people though, good stuff either way. Any solution is a good solution at this point, even if it means setting the entire form's autocomplete to off :-|
@Swader "infinite" and "setTimeout" should not be together, definitely :)
We should make the distinction between autofill and autocomplete:
- Autofill happens at page load in login forms, the browser pre-populates the form with the saved login and password. Autofill also happens in a contact form, when you start typing your name or email, some browsers (e.g. Safari) propose to complete the form based on our personal contact card from the address book.
- Autocomplete happens when you start typing the same text that has been previously submitted in the same input. Then the browser proposes the previous text that has been submitted. You may or may not use this text.
The autocomplete issue might be easier to fix, since we can trigger a model checking at the blur event. Here is a quick fix:
module.directive('input', [
function () {
return {
restrict: 'E',
require: '?ngModel',
link: function ($scope, element, attrs, controller) {
if (!attrs.type || attrs.type !== "text" || !controller) {
return;
}
element.on('blur', function () {
$scope.$apply(function () {
controller.$setViewValue(element.val());
});
});
}
};
}
]);
I don't know if the autofill issue can be easily fixed, since the browser don't seem to trigger any event during autofill (http://avernet.blogspot.in/2010/11/autocomplete-and-javascript-change.html).
I'm talking about auto-fill. Thanks for clarification.
I've posted a $100 bounty to this issue at Bountysource: https://www.bountysource.com/issues/67567-form-model-doesn-t-update-on-autocomplete
The bounty will go to the person whose pull request closes this issue.
I'd like to see this issue addressed because it affects Bountysource's signin, bountysource/frontend#78
+1
My solution is to extend the 'required' directive or whichever validation directive that might be used. I listen for the respective events that each browser throws when autocomplete is made and trigger the 'input' event.
http://jsfiddle.net/matthewgonzalez/fAmHD/10/
For us the best choice is to create a new directive at the same level than the form directive with higher priority to be executed before the controller actions and we have subscribed the event submit to refresh the value from the context before to call the actions in the controllers. At this way we always have the last values in our scope before to execute any action.
priority: 10,
link: function ($scope, element, attrs) {
//Extract
element.on('submit', function (ev) {
$('input[ng-model]', element).each(function (index, item) {
if (angular.element(this).attr('type') !== 'checkbox' && angular.element(this).attr('type') !== 'radio') {
angular.element(this).controller('ngModel').$setViewValue($(this).val());
}
});
});
//Extract
@mingue the directive approach is nice!
@mingue thanks, that's the best solution / workaround so far. solved my problems with autofill passwords.
@mingue Thanks for this directive, works really well.
Also, it seems like the bug happens in IE only when the user tries to select an autofill proposition with the mouse. I wasn't able to reproduce the bug when selecting a proposition with the keyboard, which didn't help to clearly identify it.
@matthewgonzalez Thanks for your solution! I've renamed the directive so is not replacing the 'required' validation. So far is working great.
+1
For the username/password autofill issue:
This may be a browser issue as well, I just opened a bug for Chromium:
https://code.google.com/p/chromium/issues/detail?id=307308
Meanwhile, I am using this little workaround:
$(window).load(function() {
// updates autofilled fields
window.setTimeout(function() {
$('input[ng-model]').trigger('input');
}, 100);
});
It's not very nice, if I use 2 ms instead of 100 ms it doesn't always work, which means that the autofill is performed several ms after the page has been loaded. I don't see any deterministic way to handle this problem.
For me this does the trick on FF 24.0
app.directive("autofill", function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
scope.$watch(function () {
return element.val();
}, function(nv, ov) {
if(nv !== ov)
ngModel.$setViewValue(nv)
})
}
}
});
Should be tested on other browsers, but I see no reason why it should not work... I see a couple of reasons you shouldn't use it though...
+1
+1
I'm having a problem with the workaround proposed by @mingue above. It seems to work fine in 1.0.8, but fails on 1.2.0. It seems the on('submit') never gets triggered. Does anybody have any idea as to why this is happening ?
edit : this plunk demonstrates the problem. in 1.0.8 the directive gets called first. in 1.2.0 it gets called last.
http://plnkr.co/edit/B1wWKkTdk5QHircmUdu2?p=preview
ideas ?
+1
@tech-no-logical : Did you try the code of @gplancke ? It looks perfect.
The workaround proposed by @mingue is limited : Without a submit event it's impossible to access the entry.