我们如何将参数传递给我们在Angular 2代码中为每个FormControl定义的验证方法?例如,在下面的代码中,如何将参数传递给validateSalary?像函数一样编写方法的正常方法不起作用
myForm.component.ts: ...
export class myFormComponent {
...
this.thisForm= this.builder.group({
salary: ["", ValidationService.validateSalary]
});
... }
validation.service.ts :
export class ValidationService {
...
static validateSalary (control: Control, myInput: string) { ...}
...
}
答案 0 :(得分:3)
你可能需要这样的东西:
static validateSalary(myInput: string) {
return (control: AbstractControl): {[key: string]: any} => {
// put your logic here
};
}
然后在你的Component中,声明如下:
this.thisForm = this.builder.group({
salary: ["", ValidationService.validateSalary("1000.00")]
});