填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

AsyncValidator

由执行异步验证的类实现的接口。

An interface implemented by classes that perform asynchronous validation.

      
      interface AsyncValidator extends Validator {
  validate(control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null>

  // 继承自 forms/Validator
  validate(control: AbstractControl): ValidationErrors | null
  registerOnValidatorChange(fn: () => void)?: void
}
    

方法

对提供的控件执行异步验证的方法。

Method that performs async validation against the provided control.

      
      validate(control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null>
    
参数
control AbstractControl

要验证的控件。

The control to validate against.

返回值

如果验证失败,则将解决验证错误映射表的 Promise 或 Observable,否则为 null。

Promise<ValidationErrors | null> | Observable<ValidationErrors | null>: A promise or observable that resolves a map of validation errors if validation fails, otherwise null.

使用说明

提供自定义异步验证程序指令

Provide a custom async validator directive

以下示例实现 AsyncValidator 接口,以使用自定义错误键名创建异步验证程序指令。

The following example implements the AsyncValidator interface to create an async validator directive with a custom error key.

      
      import { of } from 'rxjs';

@Directive({
  selector: '[customAsyncValidator]',
  providers: [{provide: NG_ASYNC_VALIDATORS, useExisting: CustomAsyncValidatorDirective, multi:
true}]
})
class CustomAsyncValidatorDirective implements AsyncValidator {
  validate(control: AbstractControl): Observable<ValidationErrors|null> {
    return of({'custom': true});
  }
}