Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
bind global value into ValidationErrors in angular

i try to bind value into ValidationErrors.

i have this method:

  isUniqueEmail(control: FormControl): ValidationErrors {
    if (control.value != null) {
        console.log(control.value)
        if(control.value == this.foundEmail){
          console.log("found one");
          return {isUniqueEmail: true}
        }else{
          return null;
        }
      }
  }

this method check if control.value (email typing) equal email stored in global variable this.foundEmail then we have duplicate email.

My problem is: i can retreive data from foundEmail in this method because this method is private.

this method is located inside export class exampleComponent implements OnInit.

Error: ERROR TypeError: Cannot read properties of undefined (reading 'foundEmail')

But i check i have data into foundEmail

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The validator function gets called from the FormControl so its context is not bound to the class you're defining the method on. You need to manually bind isUniqueEmail() to this.

Two options:

  1. Use bind() when defining the FormControl:

    name: new FormControl("", [
        this.isUniqueEmail.bind(this),
    ]),
    
  2. Define your validator as arrow function:

isUniqueEmail = (control: FormControl) => {
    if (control.value != null) {
        console.log(control.value)
        if(control.value == this.foundEmail){
          console.log("found one");
          return {isUniqueEmail: true}
        }else{
          return null;
        }
      }
  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!