2015-12-23 66 views
5

我有這段代碼,無論我嘗試,我無法通過以下錯誤。TypeScript'...'類型'typeof'上不存在''

錯誤: 屬性'EmailValidator'在類型'typeof UserValidators'上不存在。

代碼:

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 {}; 
    } 
} 

這是我嘗試注入爲EmailValidator:

this.fb.group({ 
     email: ['', UserValidators.EmailValidator] 
}); 
+4

該錯誤是完全正確的。這是一種實例方法;你需要一個實例。 – SLaks

+0

謝謝SLaks,我忘了它。 – alik

回答

4

你應該創建這個類的一個實例可以訪問它,就像這樣:

var userValidators : IUserValidators = new UserValidators(); 
userValidators.EmailValidator(ctrl); 
相關問題