Angularjs Chrome autocomplete dilemma

后端 未结 15 1705
野性不改
野性不改 2020-12-05 06:56

I have a simple login form which works just peachy unless you use Chrome\'s auto complete feature.

If you start typing and use the auto complete feature and it auto

15条回答
  •  失恋的感觉
    2020-12-05 07:03

    Below directive worked for me. It's simple and clean fix. Hope that helps!

    Ref: AngularJS browser autofill workaround by using a directive

    Here is a solution that is far less hacky than other solutions presented and is semantically sound AngularJS: VictorBlog.com

    myApp.directive('formAutofillFix', function() {
      return 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 auto-filled inputs
        if(attrs.ngSubmit) {
          setTimeout(function() {
            elem.unbind('submit').submit(function(e) {
              e.preventDefault();
              elem.find('input, textarea, select').trigger('input').trigger('change').trigger('keydown');
              scope.$apply(attrs.ngSubmit);
            });
          }, 0);
        }
      };
    });
    

    Then you simply attach the directive to your form:

提交回复
热议问题