Why does adding additional AngularJS validation directives cause $asyncValidators to run multiple times?

前端 未结 3 460
庸人自扰
庸人自扰 2021-01-14 09:52

Why does adding additional AngularJS validation directives cause $asyncValidators to run multiple times on page load?

I created a custom directive which

3条回答
  •  抹茶落季
    2021-01-14 10:22

    It actually took me a while to figure this one out. As mentioned in this post, Angular validators trigger additional validations. I decided not to fight this behavior and work around it instead, falling back to parsers and formatters:

    myApp.directive('userSaved',['$q','dataservice',function($q, dataservice){
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, elem, attrs, ctrl){
        ctrl.$parsers.unshift(checkUserSaved);
        ctrl.$formatters.unshift(checkUserSaved);
    
        function checkUserSaved(value){
            ctrl.$setValidity("usersaved") // the absence of the state parameter sets $pending to true for this validation
            dataservice.getUserSaved(value).then(function(response){
                var userIsSaved = (response === true);
    
                ctrl.$setValidity("usersaved", userIsSaved); // the presence of the state parameter removes $pending for this validation
    
                return userIsSaved ? value : undefined;
            });
    
            return value;
        }
      }
    }
    }]);
    

    As a reference, you also might want to check the Angular docs

    EDIT

    Upon further investigation, it appears that in the case of ng-pattern the extra validations are only triggered when the regex is converted from a string.

    Passing the regex directly:

    fixed the problem for me while making use of the validators pipeline.

    For reference, see this github issue

提交回复
热议问题