填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫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});
  }
}