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

227
Visualizações
Is it a bad practice to add HostListener to target the clicks on the document, and then check if the class matches?

Is this code considered as a bad practice:

   @HostListener('document:click', ['$event'])
      clickout(event: any) {
        if (event.target.classList.contains("my-class"))
    {
    //do somthing
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Well, yes - kind of. I mean if you could attach (click) listener directly on the target then you would only have to listen clicks on those targets, no need for additional queries either. Right now your listening to every click and then on top of that, querying classes on whatever target that click happens to land on - highly resource intensive way compared to attaching the listener on the right target in the first place.

<div class="my-class" (click)="onClick()"></div>
onClick() {
// do something
}
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