Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

267
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda