Angular 2 custom validator with parameters

后端 未结 3 1106
悲&欢浪女
悲&欢浪女 2020-12-30 21:50

How do I create let\'s say my own maxLength validator in Angular 2? All examples I could find use validators similar to \'required\' one meaning that they already know the r

3条回答
  •  -上瘾入骨i
    2020-12-30 21:55

    The minValueValidator example basically shows that you can use a factory for your custom validator so it will be something like this:

    static minValue = (num: Number) => {
        return (control:Control) => {
             var num = control.value;
             if (isNaN(num) || num < 5 ) { return {"minValue": true}; }
             return null;
        }
    }
    

    In my case I use FormControl not Control

    import { FormControl } from '@angular/forms';
    

提交回复
热议问题