我剛剛開始使用JS/Typescript和Angular 2,並且我正在努力處理以下內容。Angular 2,帶參數的自定義驗證消息
export function MinImageDimensionsValidator(minWidth: number, minHeight: number): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
// it's an image control where a user uploads an image.
// the whole image related code has been removed for better readability.
//Just assume that 'actualWidth' holds the actual width of the image
if(actualWidth < minWidth) {
return { valid: false };
}
return null;
};
}
這僅僅是一個驗證器工廠的一個非常基本的例子。
所有的例子,我發現只是寫在模板中的驗證消息/直接錯誤(我使用模板的形式)
是否可以「配合」的驗證消息的驗證器本身和使用參數與它?
喜歡:
'Min width has to be 100. you supplied ' + actualWidth
這會從驗證自身返回。
還是有另一種方式(除了將變量存儲在某處)?