Firefox中保存的密码发送空字段

时间:2014-03-18 08:55:53

标签: javascript angularjs http firefox angular-ui

我在浏览器凭据保存方面遇到问题。我第一次使用我的应用程序登录时,浏览器要求我保存字段,我按确定,但是当我第二次登录并且浏览器使用保存的凭据填写表单字段时,我按下登录,然后浏览器发送没有参数的请求。

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);
        });
    };
};

奇怪的是,如果我重写凭据一切正常,但如果我只是按下浏览器提供的凭据按钮,应用就会发送一个空白参数的帖子。

2 个答案:

答案 0 :(得分:2)

问题在于浏览器&#39;自动填充不会触发正确的事件,以便Angular可以绑定它。

一个解决方案是触发输入的更改事件,这是我从此处定制的指令(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);
    }
  };
});

该网站上发布的解决方案使用jQuery,而上述解决方案并不依赖它。

像使用任何其他指令一样使用它:

<form role="form" ng-submit="login()" form-auto-fill-fix>

答案 1 :(得分:0)

工作正常

    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);
    }
  };
});

然后

<form role="form" ng-submit="login()" formauto>