Can I programmatically apply Angular validation directives inside a custom directive?

后端 未结 5 1411
[愿得一人]
[愿得一人] 2020-12-29 20:05

I have found great many occurrences of the following pattern for html inputs, this being for phone numbers:



        
5条回答
  •  梦毁少年i
    2020-12-29 20:49

    You can try this approach:

    .directive('bkNgValidation', function () {
      return: {
        link: function (scope, element, attrs) {
          if (attrs['bk-ng-validation'] === 'phoneNumber') {
           element.$validateModel(function (value, validator) {
             if (value.length < 10 || value.length > 15) {
               validator.$setValidity('phone', true);
             } else {
               validator.$setValidity('phone', false);
             }
           });
          }
        }
      }
    })
    

提交回复
热议问题