[英](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.