【发布时间】:2019-06-07 22:40:35
【问题描述】:
我正在尝试使用自定义验证器来比较结束时间是否大于开始时间。
代码:
function timeValidator(): ValidatorFn {
return (control: AbstractControl): { [key: string]: boolean } | null => {
if (control.value !== undefined && (isNaN(control.value) || control.get('fromTime').value > control.get('toTime').value)) {
return { 'ageRange': true };
}
return null;
};
}
来自表单组
toTime: new FormControl(null, [Validators.required, timeValidator(this.fromTime,this.toTime)]),
一旦我像这样运行以下命令,我会遇到错误:Cannot read property 'value' of null 在线 if (control.value !== undefined && (isNaN(control.value) || control.get('fromTime').value > control.get('toTime').value))
我需要一些帮助来解决这个问题。谢谢
【问题讨论】:
-
你能把代码分享到stackblitz.com
-
您正在调用验证器,而不是将函数作为参数传递
-
谢谢,有一个例子很好学习。@AvinKavish
标签: angular angular7 angular-reactive-forms