Accessing class variables from form validator method in angular 2
<p>I'm trying to build a modal driven form with custom validation.
I want to Validate that the password the user insert answer the requirements (Uppercase latter,lowercase latter and number).
The issue is that I don't want to write the regex especially in the method,I want to have an ineluctable service that concentrate all of my regex.
I was able to inject the service to the component class but couldn't in any way access to the service instance inside the validetor method.</p>

<p>Any one has any idea how can I access the service?</p>

<p>Thanks.</p>

<p>This is my code :</p>

<pre><code>export class SignUpComponent implements OnInit {
regexService:RegexService
  builder: FormBuilder
  signUpForm: FormGroup;
  email: FormControl;
  password: FormControl;
  passwordAgain: FormControl;


  matchedPassword (input: any) {
    if(!input.root || !input.root.controls)
    return null;

    if(input.root.controls.password.value===input.root.controls.passwordAgain.value)
      return null;
    else
      return { mismatchedPassword: true };
   }

   passwordAnswerRequirements(input: any){
     if(!input.root || !input.root.controls)
     return null;

     if(this.regexService.getRegex('password').test(input.value))
        return null;
      else
        return {notAcceptablePassword:true}
   }



  constructor(builder: FormBuilder,regexService:RegexService) {
    this.builder=builder;
    this.regexService=regexService;
  }

  ngOnInit(){
    this.email = new FormControl('', [
       Validators.required,
    ]);
     this.password = new FormControl('', [
        Validators.required,
        this.matchedPassword,
        this.passwordAnswerRequirements
     ]);
     this.passwordAgain=new FormControl('',[
        Validators.required,
        this.matchedPassword,
        this.passwordAnswerRequirements
     ]);
     this.signUpForm = this.builder.group({
       email: this.email,
       password: this.password,
       passwordAgain:this.passwordAgain
     });
  }
</code></pre>

<p>My service name is 'RegexService' and the validetor function name is 'passwordAnswerRequirements'</p>