首页 > 解决方案 > 如何将数组发送到角度自定义验证器

问题描述

这是我的验证器功能。我需要从我的组件传递条形码数组。我尝试了很多方法,但没有任何效果。

 checkBarcodes(control: AbstractControl): { [key: string]: boolean } | null {
        let barcodes = ["6456", "6545", "2", "2", "121", "22", "11111", "22222"]
        for (let code of barcodes) {
            if (code == control.value) {
                return {
                    isValid: true
                }
            }
        }
        return null
    }

标签: angularangular-formsangular-formbuilder

解决方案


export function checkBarcodes(barcodes): ValidatorFn  {

    return (control : AbstractControl) : ({ [key: string]: boolean } | null) => {
        for (let code of barcodes) {
            if (code === control.value) {
                return  null ;
            }
        }
        return {'codeInvalid': true} ;
    }
}

推荐阅读