gpt4 book ai didi

使用动态/更新参数的 Angular 7 自定义验证

转载 作者:行者123 更新时间:2023-12-02 10:06:06 25 4
gpt4 key购买 nike

我正在使用带有 Material 设计组件的 Angular 7

我需要将 requireMatch 验证添加到 mat-autocomplete 中。

我已经使用参数创建了自定义验证,但参数值确实会动态更改。

下面是我的组件代码。

this.stepFormGroup = this.formBuilder.group({
AccessCode: ["", [Validators.required, this.requireMatch(this.accessCodeList)]]
});

////require-match validation for access-code
requireMatch = (accessCodes: string[]) => {
return (control: FormControl) => {
const selection: any = control.value;
console.log("accessCodes", accessCodes, "selection", selection);
if (accessCodes.indexOf(selection)===-1) {
return { requireMatch: true };
}
return null;
}
}

我面临的问题是,我总是在 requireMatch 内的 accessCodes 中得到空(init)。

this.accessCodeList 的更改不会反射(reflect)到验证器。

这意味着更改 this.accessCodeList 后,它不会在 requireMatch 验证器中获得更新的数组。

那么有人知道如何在自定义验证器中传递动态参数吗?

最佳答案

这样调用时需要绑定(bind)验证函数,否则验证函数不会绑定(bind)输入accessList

[Validators.required, this.requireMatch(this.accessCodeList).bind(this)]

此外,如果您想限制该字段中的某些单词,您可以在此处查看我的 npm 包之一 https://www.npmjs.com/package/ng4-validation

关于使用动态/更新参数的 Angular 7 自定义验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55282111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com