Angular 2 FormControl,验证方法参数传递

时间:2016-07-31 02:44:56

标签: javascript typescript angular

我们如何将参数传递给我们在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) { ...}
   ...
}

1 个答案:

答案 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")]
});