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

166
Visualizações
Angular arrow function, "this" is undefined

I am using Angular and call another function that makes an api call to check something. When I call the function, the this inside it is undefined!? why would this be? and how do I fix it? is my arrow function call incorrect?

This is the function that is calling the arrowFunc

onProductChanged(event) {
  this.arrowFunc(hasRangeConfigurations => {
    console.log("do something");
  })
}

This the the function that the above calls, the "this" is undefined in the const declaration at this point, but in the above it is a reference to my angular component.

arrowFunc(action: (hasRangeConfigurations: boolean) => void) {
    const criteria = {
        brandId: this.marketingRange?.brand?.id,
        pageNumber: 1,
        pageSize: 1,
    } as Myresults
    
    // extra code here
});

Interestingly this does exist outside the const but inside the const it is undefined!?

-- UPDATE My Solution--

Well before you know it someone deletes your question and forwards you to something else that is confusing.

As I cant ask a legitimate question as it gets deleted, I solved it this way, just assigning this to self (like I had done back in the jquery/javascript days) I dont know if this is the best solution, but hey hoo!

arrowFunc(action: (hasRangeConfigurations: boolean) => void) {
        const self = this;
        const criteria = {
            brandId: self.marketingRange?.brand?.id,
            pageNumber: 1,
            pageSize: 1,
        } as Myresults
        
        // extra code here
    });
about 4 years ago · Juan Pablo Isaza
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