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

+1

jstockdi avatar Jan 03 '14 20:01 jstockdi

+1

ecaron avatar Jan 07 '14 16:01 ecaron

I too would really like to see this fixed and just magically working in angular. Since that hasn't happened yet, I decided to create a simple directive to solve the situation I needed.

It's available here for anyone interested: https://github.com/chesleybrown/autofill-directive

Basically I just needed the model to be updated once the user submitted the form (I didn't need the model to be updated instantly upon the browser autofill on page load). This worked perfectly for what I see as the most common case: the login form. Once the user submits the login form, the model is updated with what the browser autofilled in and the user is logged in.

Hopefully some of you find it useful too. :)

chesleybrown avatar Jan 07 '14 23:01 chesleybrown

+1

robianmcd avatar Jan 08 '14 19:01 robianmcd

+1

chinchang avatar Jan 09 '14 09:01 chinchang

@chesleybrown is worked on 1.2.7

guanbo avatar Jan 09 '14 10:01 guanbo

I am using 1.2.7 but the issue still exists.

I am using autocomplete="off" now on a form, which is a crap solution, but at least it is clear for the user that autocomplete doesnt work.

soudmaijer avatar Jan 09 '14 14:01 soudmaijer

@soudmaijer I think that @guanbo is talking about the directive that @chesleybrown made

cbier avatar Jan 09 '14 15:01 cbier

~~The directive from @chesleybrown helped me fix the issue for Chrome (against 1.2.7), but Firefox users w/ autofill are still feeling the burn.~~

Revoking previous comment. @chesleybrown implementation still works in FF, I was using it wrong.

ecaron avatar Jan 09 '14 15:01 ecaron

Are you sure @ecaron? It's working in Firefox for me. That's how I originally coded it (I haven't even tested it in Chrome yet, but thanks for letting me know it works there :smile_cat: ). Maybe a caching problem?

chesleybrown avatar Jan 09 '14 15:01 chesleybrown

Thanks @speg but your PR depends on jQuery, so it's will be impossible to merge-it to angular.js

GabrielDelepine avatar Jan 10 '14 12:01 GabrielDelepine

@Yappli sorry, I was just referencing this issue in a PR to another repo. Didn't intend it to be a PR to angular.js

speg avatar Jan 10 '14 12:01 speg

Short update: We are working on a polyfill that triggers the change event at the right time so everything is find, also for login forms.

This will be a separate javascript file that needs to be loaded and which patches jQuery / jQLite.

Almost done, only the test cases can't be automated (will be semi-automated with manual steps in between).

tbosch avatar Jan 11 '14 00:01 tbosch

@tbosch Awesome!! Thanks!!!! :D

marcalj avatar Jan 11 '14 10:01 marcalj

I wrote a solution to this here that is semantically sound AngularJS: http://victorblog.com/2014/01/12/fixing-autocomplete-autofill-on-angularjs-form-submit/

evictor avatar Jan 12 '14 09:01 evictor

@evictor on-submit directives are useless (too simple to implement without directive). Main problem is triggering events on change, to check user's input before submit.

e-oz avatar Jan 12 '14 09:01 e-oz

+1

jvdvleuten avatar Jan 15 '14 18:01 jvdvleuten

@tbosch The 1.2.9 milestone is closed. Does that means this will probably be in 1.2.10?

blaise-io avatar Jan 16 '14 09:01 blaise-io

+1

friksa avatar Jan 16 '14 10:01 friksa

For who want to fix that annoying thing, I've made a decoration of inputDirective, based on https://github.com/angular/angular.js/issues/1460#issuecomment-28662156:

// Due to browsers issue, it's impossible to detect without a timeout any changes of autofilled inputs
// https://github.com/angular/angular.js/issues/1460
// https://github.com/angular/angular.js/issues/1460#issuecomment-28662156
// Could break future Angular releases (if use `compile()` instead of `link())
// TODO support select
angular.module("app").config(["$provide", function($provide) {
    var inputDecoration = ["$delegate", "inputsWatcher", function($delegate, inputsWatcher) {
        var directive = $delegate[0];
        var link = directive.link;

        function linkDecoration(scope, element, attrs, ngModel){
            var handler;
            // By default model.$viewValue is equals to undefined
            if(attrs.type == "checkbox"){
                inputsWatcher.registerInput(handler = function(){
                    var value = element[0].checked;
                    // By default element is not checked
                    if (value && ngModel.$viewValue !== value) {
                        ngModel.$setViewValue(value);
                    }
                });
            }else if(attrs.type == "radio"){
                inputsWatcher.registerInput(handler = function(){
                    var value = attrs.value;
                    // By default element is not checked
                    if (element[0].checked && ngModel.$viewValue !== value) {
                        ngModel.$setViewValue(value);
                    }
                });
            }else{
                inputsWatcher.registerInput(handler = function(){
                    var value = element.val();
                    // By default value is an empty string
                    if ((ngModel.$viewValue !== undefined || value !== "") && ngModel.$viewValue !== value) {
                        ngModel.$setViewValue(value);
                    }
                });
            }

            scope.$on("$destroy", function(){
                inputsWatcher.unregisterInput(handler);
            });

            // Exec original `link()`
            link.apply(this, [].slice.call(arguments, 0));
        }

        // Decorate `link()` don't work for `inputDirective` (why?)
        /*
         directive.link = linkDecoration;
         */
        // So use `compile()` instead
        directive.compile = function compile(element, attrs, transclude){
            return linkDecoration;
        };
        delete directive.link;

        return $delegate;
    }];

    $provide.decorator("inputDirective", inputDecoration);
    $provide.decorator("textareaDirective", inputDecoration);
    //TODO decorate selectDirective (see binding "change" for `Single()` and `Multiple()`)
}]).factory("inputsWatcher", ["$interval", "$rootScope", function($interval, $rootScope){
    var INTERVAL_MS = 500;
    var promise;
    var handlers = [];

    function execHandlers(){
        for(var i = 0, l = handlers.length; i < l; i++){
            handlers[i]();
        }
    }

    return {
        registerInput: function registerInput(handler){
            if(handlers.push(handler) == 1){
                promise = $interval(execHandlers, INTERVAL_MS);
            }
        },
        unregisterInput: function unregisterInput(handler){
            handlers.splice(handlers.indexOf(handler), 1);
            if(handlers.length == 0){
                $interval.cancel(promise);
            }
        }
    }
}]);

Update code: add inputsWatcher + fix support checkoxes and radios buttons

mems avatar Jan 16 '14 17:01 mems