简体   繁体   English

(控件:AbstractControl):

[英](control: AbstractControl) :

Here a snippet of code extract from https://angular.io/guide/form-validation#custom-validators 这是从https://angular.io/guide/form-validation#custom-validators提取的代码片段

/** A hero's name can't match the given regular expression */
export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn {
   return (control: AbstractControl): {[key: string]: any} => {
       const forbidden = nameRe.test(control.value);
       return forbidden ? {'forbiddenName': {value: control.value}} : null;
  };
}

What is happening at the third line : 第三行发生了什么:

 return (control: AbstractControl): () => {}

Is it typing what the lambda function will return ? 是否输入lambda函数将返回什么? forbiddenNameValidator is supposed to return a ValidatorFn, should ValidatorFn be understood as ValidatorFonction ? forbiddenNameValidator应该返回ValidatorFn,ValidatorFn应该被理解为ValidatorFonction吗?

If you study the definition of ValidatorFn interface it has the following pattern - 如果您研究ValidatorFn接口的定义,则它具有以下模式-

interface ValidatorFn {
  (c: AbstractControl): ValidationErrors | null
}

which mean you can return any function which accepts an argument of type AbstractControl and returns either type ValidationErrors (which is actually a type alias) or null . 这意味着您可以返回任何接受类型为AbstractControl的参数并返回ValidationErrors类型(实际上是类型别名)或null

Here is the definition of ValidationErrors , it has the below index signature 这是ValidationErrors的定义,它具有以下索引签名

type ValidationErrors = {
    [key: string]: any;
};

So your below example 所以你下面的例子

return (control: AbstractControl): {[key: string]: any} => { 
 // other code 
 return forbidden ? {'forbiddenName': {value: control.value}} : null; // check this line it's corresponds to ValidationErrors | null
}

actually returns a function of same signature that is compatible with the pattern of ValidatorFn interface. 实际上返回具有与ValidatorFn接口模式兼容的相同签名的函数。

(control: AbstractControl): {[key: string]: any} => 
       {
         return forbidden ? {'forbiddenName': {value: control.value}} : null; 
       }

check this link for reference : https://angular.io/api/forms/ValidatorFn#call 检查此链接以供参考: https : //angular.io/api/forms/ValidatorFn#call

So the idea behind forbiddenNameValidator function is to return a function with same signature as the property of ValidatorFn interface 因此, forbiddenNameValidator函数背后的想法是返回一个与ValidatorFn接口的属性具有相同签名的函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AbstractControl.value[data] 正在使用 AbstractControl 重新调整自定义验证器中的未定义? - AbstractControl.value[data] is retuning undefined in Custom Validator using AbstractControl? 类型“ AbstractControl”上不存在属性“控件”。 角度7 - Property 'controls' does not exist on type 'AbstractControl'. Angular 7 角度错误:类型“void”不可分配给类型“AbstractControl” - Angular Error : Type 'void' is not assignable to type 'AbstractControl' 循环使用Angular中FormGroup中的子AbstractControl列表 - Looping on child AbstractControl list within FormGroup in Angular 从 Angular 反应式 FormArray 获取同级 AbstractControl - 自定义验证 - Get sibling AbstractControl from Angular reactive FormArray - Custom Validation 选择选项的自定义属性,并从Angular 5中的AbstractControl中访问它? - Custom attribute for select option and accessing it from AbstractControl in Angular 5? 类型“ string”的参数不能分配给“ AbstractControl”类型的参数。 角反应形式 - Argument of type 'string' is not assignable to parameter of type 'AbstractControl'. Angular Reactive Forms 在“{ [key: string]: AbstractControl; 类型上找不到带有“string”类型参数的索引签名 } - No index signature with a parameter of type 'string' was found on type '{ [key: string]: AbstractControl; } “AbstractControl”类型上不存在属性“控件”。ngtsc(2339) - Property 'controls' does not exist on type 'AbstractControl'.ngtsc(2339) 如何通过 Angular 9 中的 FormGroup (AbstractControl) 在 Reactive Form 中重置 ng-select? - How to reset ng-select in Reactive Form through a FormGroup (AbstractControl) in Angular 9?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM