将自定义验证器拆分为多个函数Angular 2

问题描述 投票:0回答:1
    export class FormFieldErrorExample {
      email = new FormControl('', [Validators.required, Validators.email, this.lengthValidator]);

      getErrorMessage() {
        return this.email.hasError('required') ? 'You must enter a value' :
            this.email.hasError('email') ? 'Not a valid email' :
                'minimum length should be greater than 10';
      }
      lengthValidator(control : AbstractControl){

        if(control.value.length <10)    
        return {lengthError : true};
        else return this.anotherValidator(control);
      }
anotherValidator(control: AbstractControl){

 return {lengthError : true};
}

我试图将验证器拆分成另一个函数,但是得到一个错误,比如'无法读取属性'另一个未定义的'寡核苷酸'。如何将验证器拆分为多个函数并正确传递控件

https://stackblitz.com/edit/angular-psfasq-5yxaln

angular angular2-forms angular-reactive-forms angular-validation
1个回答
1
投票

你应该使用thislengthValidator的上下文传递给你的customvalidator bind()

email = new FormControl('', [Validators.required, Validators.email, this.lengthValidator.bind(this)])
© www.soinside.com 2019 - 2024. All rights reserved.