2

I have a problem with the browsers credentials saving. The first time that I login with my app, the browser ask me to save the fields, I press ok, but when I login the second time and the browser fullfill the form fields with the saved credentials, I press login, and the browser send a request without parameters.

HTML

<div ng-controller="modalCtrl">
    <script type="text/ng-template" id="login">
    <form ng-submit="login()" class="login">
        <input type="text" ng-model="data.username" placeholder="username" value="username" class="form-control" popover="inserisci qui il tuo username" popover-trigger="focus" popover-placement="right"/><br>
        <input id="pwd" type="password" ng-model="data.password" placeholder="password" value="password" class="form-control" popover="inserisci qua la tua password" popover-trigger="focus" popover-placement="right"/><br>
        <input class="btn-primary btn-lg" type="submit" value="Login">
        </form>
    </script>
</div>

JS

modalController.controller('modalCtrl',function ($scope, $modal) {
var open = function () {
    var modalInstance = $modal.open({
        templateUrl: 'login',
        controller:this.loginCtrl,
        backdrop:'static'
        });
};
open();

});
var loginCtrl = function ($scope, $http, $modalInstance, $state) {  
    $scope.data = {username: this.username, password: this.password};
    var data = $scope.data;
    $scope.login = function () {
    $http.post(server[0] + '/bbi-api/sauth/1', {name: data.username, password: data.password})
        .then(function () {
            $state.go('home');
            $modalInstance.close();

        },
        function (response) {
            alert(response.status);
        });
    };
};

The strange is that if I rewrite the credentials everything goes ok, but if I simply press the button with the credentials put by the browser, the app send a post with blank parameters.

fzzle
  • 1,466
  • 5
  • 23
  • 28
mautrok
  • 961
  • 1
  • 17
  • 41

2 Answers2

2

The problem is that the browsers' autofill is not triggering the correct event so that Angular can bind to it.

One solution is triggering a change event for your inputs, here is a directive I customized from here (http://victorblog.com/2014/01/12/fixing-autocomplete-autofill-on-angularjs-form-submit/):

Coffeescript:

angular.module('app')
  .directive 'formAutoFillFix', ->
    (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 ->
          elem.unbind('submit').bind 'submit', (e) ->
            e.preventDefault()

            elem.find('input').triggerHandler('change')

            scope.$apply attrs.ngSubmit
        , 0

Javascript:

angular.module('app').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').bind('submit', function(e) {
          e.preventDefault();
          elem.find('input').triggerHandler('change');
          scope.$apply(attrs.ngSubmit);
        });
      }, 0);
    }
  };
});

The solution posted on that website uses jQuery, whereas the above solution does not rely on it.

Use it as you would any other directive:

<form role="form" ng-submit="login()" form-auto-fill-fix>
StuR
  • 12,042
  • 9
  • 45
  • 66
  • Thanks StuR, i couldn't try your solution before, now my problem is solved, but you have to edit your answer in the html's part, it's "form-autofill-fix" not "form-auto-fill-fix", in the second way the solution doesn't work. Thanks anyway. – mautrok Mar 21 '14 at 13:02
  • Sorry to ask after a long time! I have the same problem and I tried your solution, but I don't know how to make it work. I just see `form-auto-fill-fix=""` as an attribute to my form. – Rutwick Gangurde Aug 08 '14 at 10:13
0

That works fine

    myApp.directive('formauto', 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);
    }
  };
});

and then

<form role="form" ng-submit="login()" formauto>
Silvio Troia
  • 1,439
  • 19
  • 36