类型'typeof ...'上不存在TypeScript'...'

ali*_*lik 14 typescript angular

我有这段代码,无论我尝试什么,我都无法通过以下错误.

错误: 类型'typeof UserValidators'上不存在属性'EmailValidator'.

码:

import {EMAIL_REGEX} from '../constants';
import {Control} from 'angular2/common';

export interface IUserValidators {
  EmailValidator(control: Control) : Object;
}

export class UserValidators implements IUserValidators {
  EmailValidator(control: Control) : Object {
    if (!control.value) {
      return {
        required: true
      };
    } else if (control.value) {
      if (!new RegExp(EMAIL_REGEX).test(control.value)) {
        return {
          invalid: true
        };
      }
    }
    return {};
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试注入EmailValidator的方式:

this.fb.group({
      email: ['', UserValidators.EmailValidator]
});
Run Code Online (Sandbox Code Playgroud)

gil*_*ran 18

您应该创建此类的实例以便能够访问它,如下所示:

var userValidators : IUserValidators = new UserValidators();
userValidators.EmailValidator(ctrl);
Run Code Online (Sandbox Code Playgroud)