如何在Angular表单中使用setValidator设置自定义验证器?

时间:2018-07-26 22:00:43

标签: angular angular-reactive-forms

这是我为测试编写的验证器。

const checkValidator = (control: AbstractControl) => {
  console.log(control);
  return true;
};

PS:它在组件类的外部,因此const可以工作。

现在,我正在尝试将其设置为表单中的控件。

this.fieldForm.get('propertiesDAMSource').setValidators([checkValidator]);

我收到此错误。

Argument of type '((control: AbstractControl) => boolean)[]' is not assignable to parameter of type 'ValidatorFn | ValidatorFn[]'.
  Type '((control: AbstractControl) => boolean)[]' is not assignable to type 'ValidatorFn[]'.
    Type '(control: AbstractControl) => boolean' is not assignable to type 'ValidatorFn'.
      Type 'boolean' is not assignable to type 'ValidationErrors'.

1 个答案:

答案 0 :(得分:0)

尝试返回如下所示的内容:

return { 'range': true };

“范围”是验证的名称,而不是返回布尔值。

相关问题