动态生成的复选框的自定义验证器不起作用
我已经在Angular 13版本中创建了一个项目,以验证动态生成的复选框。我已经关注了这个博客( https://coryrylan.com/博客/create-a-dynamic-checkbox-list-in-Angular )。在此博客中,有一个自定义验证。但是验证对我不起作用。自定义验证器代码是:
function minSelectedCheckboxes(min = 1) {
const myValidator: ValidatorFn = (formArray: FormArray) => {
const totalSelected = formArray.controls
.map(control => control.value)
.reduce((prev, next) => next ? prev + next : prev, 0);
return totalSelected >= min ? null : { required: true };
};
return myValidator;
}
但是我遇到以下错误。谁能帮助我解决错误?
src/app/app.component.ts:62:8-错误ts2322:type'(formarray:formarray: formarray)=> {必需:布尔值; } | null'不能分配输入 “验证器”。参数类型“ formarray”和“控制”是 不相容。 类型“ AbstractControl”缺少“ formarray”类型的以下属性:控制,at,pusp,插入和6个。
62 const myValidator:validatorFn =(formArray:formArray)=> { ~~~~~~~~~~~
net/d1qe4.png“ alt =”在此处输入图像描述>
I have created a project in Angular 13 version for validating the dynamically generated checkboxes. I have followed this blog (https://coryrylan.com/blog/creating-a-dynamic-checkbox-list-in-angular). In this blog, there is a custom validation. But the validation is not working for me. The custom validator code is:
function minSelectedCheckboxes(min = 1) {
const myValidator: ValidatorFn = (formArray: FormArray) => {
const totalSelected = formArray.controls
.map(control => control.value)
.reduce((prev, next) => next ? prev + next : prev, 0);
return totalSelected >= min ? null : { required: true };
};
return myValidator;
}
But I am getting the below error. Can anyone help me to solve the error?
src/app/app.component.ts:62:8 - error TS2322: Type '(formArray:
FormArray) => { required: boolean; } | null' is not assignable to type
'ValidatorFn'. Types of parameters 'formArray' and 'control' are
incompatible.
Type 'AbstractControl' is missing the following properties from type 'FormArray': controls, at, push, insert, and 6 more.62 const myValidator: ValidatorFn = (formArray: FormArray) => {
~~~~~~~~~~~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
验证器
具有以下类型:因此您需要更改这样的功能:
ValidatorFn
has the following type:So you need to change the function like this: