我创建了一个用于表单验证的角度验证器模块,而无需在表单中包含ngMessages,并且一切正常。

javascript - AngularJS指令$ compile导致ng-click触发两次-LMLPHP

但是我发现了一个我一直试图修复的错误。它与$compile有关。

因此,我有一条向表单元素添加属性的指令,这是通过使用$ compile服务实现的,但是,似乎$ compile服务会对ng-click造成有害行为,因此当调用位于表单内的ng-click时,它将触发两次;

这是指令以及我在做什么:

angular.module('app',[])

.directive('validateForm',['$compile',
        function($compile)
        {
            var addErrors = function(rules, form, ctrls, scope, config){
                //code
            };


            return {
                restrict: 'A',
                require: ['^form'],
                link: {
                    post: function(scope, element, attrs, ctrls){

                        var form = ctrls[0];
                        var config = scope.validator;

                        if(typeof config != 'object') return;

                        var rules = config['rules'];
                        var errors = [];

                        //-----
                    },
                    pre: function(scope, element, attrs, ctrls){

                        var elm = element.find('select, input, textarea').attr('validate-field','');
                        element.removeAttr("validate-form"); //remove the attribute to avoid indefinite loop
                        element.removeAttr("data-validate-form");
                        $compile(element.contents())(scope);
                    }
                }
            };
        }

    ])
 .controller('UserController',['$scope', function($scope){
  $scope.title = 'Form Validator';
  $scope.clickThings = function(value){
    alert(value); //pops up twice means ng-click fires twice
  }
}]);


表单标记:

 <div ng-controller="UserController">
 <form novalidate="" validate-form name="form" role="form">
     <div class="form-group">
          <input type="text" class="form-control" ng-model="first_name" name="first_name" />
        </div>
        <div class="form-group">
          <input type="text" class="form-control" ng-model="last_name" name="last_name" />
        </div>
        <div class="form-group">
          <input type="text" class="form-control" ng-model="email" name="email" />
        </div>
        <div class="form-group">
          <input type="password" class="form-control" ng-model="password" name="password" />
        </div>
        <div class="form-group">
          <input type="text" class="form-control" ng-model="country" name="country" />
        </div>
        <a type="button" class="btn btn-success" ng-click="clickThings('Submit Clicked')">Submit</a>
      </form>
      </div>


我创建了一个插件:
http://embed.plnkr.co/uIid4gczKxKI4rPOHqx7

最佳答案

在尝试了不同的事情之后,我意识到当调用ng-click时第二次编译已经编译的$compile(element.contents())(scope)

要解决此问题,只需编译更改/受影响的元素,即
elem = element.find('select, input, textarea').attr('validate-field','');

通过将$compile(element.contents())(scope)替换为$compile(elem)(scope);

所以我最终得到了这个:

angular.module('app',[])

.directive('validateForm',['$compile',
    function($compile)
    {
        var addErrors = function(rules, form, ctrls, scope, config){
            //code
        };

        return {
            restrict: 'A',
            require: ['^form'],
            link: {
                post: function(scope, element, attrs, ctrls){

                    var form = ctrls[0];
                    var config = scope.validator;

                    if(typeof config != 'object') return;

                    var rules = config['rules'];
                    var errors = [];

                    //-----
                },
                pre: function(scope, element, attrs, ctrls){

                    var elem = element.find('select, input, textarea').attr('validate-field','');
                    element.removeAttr("validate-form"); //remove the attribute to avoid indefinite loop
                    element.removeAttr("data-validate-form");
                    $compile(elem)(scope);
                }
            }
        };
    }

 ])
 .controller('UserController',['$scope', function($scope){
   $scope.title = 'Form Validator';
   $scope.clickThings = function(value){
   alert(value); //pops up twice means ng-click fires twice
  }
 }]);


这里的工作插件:

关于javascript - AngularJS指令$ compile导致ng-click触发两次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38373671/

10-12 15:12