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

前端 未结 3 507
庸人自扰
庸人自扰 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:34

    This is because validation directives like ngMaxlength, ngPattern invoke an initial validation cycle with a call to ngModelController.$validate().

    This causes all the validation directive to run their validation logic, including the async validators.

    One way to prevent the redundant $http calls, and in fact it is a good practice anyway, is to cache the validation result for each input.

提交回复
热议问题