AngularJS - using a form and auto-completion

前端 未结 2 672
萌比男神i
萌比男神i 2021-01-06 08:23

I have the following code in a partial page used for login...

相关标签:
2条回答
  • 2021-01-06 09:03

    Here is an alternative solution that is semantically sound AngularJS: http://victorblog.com/2014/01/12/fixing-autocomplete-autofill-on-angularjs-form-submit/

    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 autofilled 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 attach the directive to your form:

    <form ng-submit="submitLoginForm()" form-autofill-fix>
      <div>
        <input type="email" ng-model="email" ng-required />
        <input type="password" ng-model="password" ng-required />
        <button type="submit">Log In</button>
      </div>
    </form>
    
    0 讨论(0)
  • 2021-01-06 09:05

    A nice thing about autocomplete is that it leaves the form $pristine.

    You can do a simple check and get the value when needed:

    if($scope.[form_name].$pristine){
       var input document.getElementById([input_id]);
       var input_value = input.value
    }
    

    You can then submit the value or replace it e.g $scope.[module] = input.value

    0 讨论(0)
提交回复
热议问题