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

and is very different from detecting "autofill"

It's more like a primitive version of Object.observe, and you'd run into bugs whenever any property other than value on the input changed (I'd also be surprised if changing the value from script context didn't queue up dispatch of another propertychange event, unless it's even hackier than I thought). Plus, this change would still only apply to input elements, not select elements (for example). And even for input elements, it would not even remotely benefit the vast majority of users.

@tbosch's library is a better solution for this, imo.

caitp avatar Jul 01 '14 11:07 caitp

you'd run into bugs whenever any property other than value on the input changed this change would still only apply to input elements, not select elements (for example)

for this event, we can add a wrapper function to fire the original deferListener for input element and value property only.

it would not even remotely benefit the vast majority of users.

since this event is IE 8 only, which means at least IE8 users would benefit from this event and without any side effect for others, imo

and is very different from detecting "autofill"

at the msdn page I pasted, there is a statement which declared that "onpropertychange" is the official way to detect autofill: To determine when a user updates the content of a field from the AutoComplete dialog box, use the onpropertychange event, rather than the onchange event, because the onchange event does not fire. so I don't think using this event is evil.

xzer avatar Jul 01 '14 11:07 xzer

+1

vipulsodha-zz avatar Jul 04 '14 22:07 vipulsodha-zz

@xzer Angularjs is a general-purpose library used by the whole world. Whole world meaning X browsers and their Y versions. Adding code which is known to only be for IE8 is not a very good idea in this regard, is it? Anyways, Angular 1.3 dropped support for IE8 (rightly so), and thus as far as I know, no steps would be taken in this regard from the Angular team.

As a workaround, you can monkeypatch your own angularjs library to include support for IE8 if you wish to do so.

kumarharsh avatar Jul 05 '14 05:07 kumarharsh

@kumarharsh I agree with your opinion that angular js is designed for all browsers and all versions. I also think this bug fix is not necessary for the newest angular js version. But as a matter of fact, the current stable version shown on the office site is 1.2.x which declares supporting IE 7 & 8. For the old ages, IE only codes, Firefox only codes, Chrome only codes, we were suffering from all of those magic things, I understand it, and the current situation is that there are many developer who are still suffering from those things because the damned XP and old IE which are still used by many big companies, I believe the terrible situation would still continue for about 1-2 years.

In conclusion, I believe it is worth fixing this problem in before 1.2.x versions officially, not my own monkeypatch.

xzer avatar Jul 07 '14 02:07 xzer

@kumarharsh I actually disagree with your sentiment- nobody likes having to support IE , but that doesn't mean that we should make the users of our websites suffer by choosing to not implement a bug fix. When Internet Explorer was the master, we had to support Chrome, Safari and Firefox users; now that Chrome has all but annihilated every other modern browser in the landscape ( except for Firefox :-P ) we still need to support IE8 until XP dies. You don't win users by forcing them to do something (see: Microsoft), you win them by showing them a better way (see: Google / Apple). Chromeframe is a HUGE reason why Chrome has taken over.

TLDR:

it's a bug fix that supports a browser that a few million people still use so a lot of us still have to deal with. If it helps some people, hurts no one, and the solution is in your hands- which is what @xzer appears to be asserting - then why stand in the way?

andrew-luhring avatar Jul 08 '14 04:07 andrew-luhring

There are a few bug reports filed for Firefox. I've filed one back in 2008 - https://bugzilla.mozilla.org/show_bug.cgi?id=430931, and there's one recent specific to this thread https://bugzilla.mozilla.org/show_bug.cgi?id=950510. Feel free to click "vote" and comment.

naktinis avatar Jul 11 '14 10:07 naktinis

+1 for the issue. The auto-completed fields have blank values in the models. I tried using some of the directives posted by other users here. But they never work. Here is a simple fix that I used to get around this problem. In your controller, check if the details exist in the model, if not use the raw form field values. Example, if your model is car, and the field is car.name, check if the value is empty in the controller, and use the raw text input value [$(...).val()] if yes. Then set the value for $scope.car in your controller.

(Pardon my terminology, I'm new to Angular)

rutwick avatar Aug 11 '14 11:08 rutwick

my workaround

var myAutofillDirective = [ function() {
    return {
        require: 'ngModel',
        link:function(scope, element, attr, ngModel) {
            var origVal = element.val();
            if(origVal){
                ngModel.$modelValue = ngModel.$modelValue || origVal;
            }
        }
    };
} ];    

swaron avatar Aug 21 '14 00:08 swaron

I had to modify @evictor's solution a tad, since I am not using jQuery, and thus don't have .trigger. Also, I only have <input> in my form, and the find('input, select, textarea') did not return anything for me, so I only search for input.

angular.directive('autofillFix', function () {

    return {
      link: function(scope, elem, attrs) {
        // Fixes Chrome bug: https://groups.google.com/forum/#!topic/angular/6NlucSskQjY
        elem.prop('method', 'POST');

        // Fix autofill issues where Angular doesn't know about autofilled inputs
        if(attrs.ngSubmit) {
          setTimeout(function() {
            elem.unbind('submit').bind('submit', function (e) {
              e.preventDefault();
              var arr = elem.find('input');
              if (arr.length > 0) {
                arr.triggerHandler('input').triggerHandler('change').triggerHandler('keydown');
                scope.$apply(attrs.ngSubmit);
              }
            });
          }, 0);
        }
      }
    };
  });

davisford avatar Aug 26 '14 17:08 davisford

Grrrr, none of the above suggestions worked for me. AngularJS really should not make it more complicated for us. AngularJS shouldn't obstruct the autocompleter of a browser.

binarykitchen avatar Aug 29 '14 06:08 binarykitchen

@binarykitchen my dirty hack (https://github.com/angular/angular.js/issues/1460#issuecomment-18579638) still works fine :) And it's not AngularJS' problem - browsers just don't send events about autocompletion, so blame browsers. Or fork-fix them and send PR )

e-oz avatar Aug 29 '14 10:08 e-oz

"Browsers just don't send events about autocompletion". Exactly, so it's ridiculous for AngularJS to assume that they do.

dlitz avatar Aug 29 '14 22:08 dlitz

AngularJS is a product of Googe so it does make at least a little bit of sense that they do.

andrew-luhring avatar Aug 29 '14 23:08 andrew-luhring

So it's a browser issue then? Is there a ticket for that on the Google Chrome issue tracker?

binarykitchen avatar Aug 30 '14 03:08 binarykitchen

@binarykitchen may I suggest you at least try to search before writing message that are emailed to 108 people. In this very thread the ticket were already mentioned (CTRL+F magic):

Chrome: https://code.google.com/p/chromium/issues/detail?id=135307 Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=430931 https://bugzilla.mozilla.org/show_bug.cgi?id=950510

Also @tbosch may I suggest you lock this thread ? I feel that everything that could be said was already said, with numerous workarounds mentioned. Most notably https://github.com/tbosch/autofill-event which is something that should work for every cases. The discussion is no longer constructive IMHO.

PowerKiKi avatar Aug 30 '14 03:08 PowerKiKi

I agree with @PowerKiKi, locking this thread now.

tbosch avatar Sep 02 '14 17:09 tbosch

Just an update that the issue is fixed in Chrome M40. Maybe even M39.

There are still issues with Safari 7 and 8. In FF34 the forms autofill works well, but password autofill is still broken.

IgorMinar avatar Dec 03 '14 21:12 IgorMinar