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

243
Visualizações
Angular 11 - HostListener event click and get value object

I am using the HostListener directive, to listen for the click event on elements of the DOM.

@HostListener('click', ['$event.target']) onClick(e) {
  console.log("event", e)
}

When I click on a button tag, the "e" object has the following:

<button _ngcontent-abr-c111 type="button" class="close ng-tns-c111-3">x</button>

If I loop through the object, it shows the following:

  const objectList = Object.keys(e).map(k => (
    console.log("key", k)
  ));

// result = key __zone_symbol__clickfalse

But what I would like to ask in my code is, if the button element has the "close" class, if it does, I will do one thing and if not another.

How could I do it? Thanks,"

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use classList

@HostListener('click', ['$event.target']) onClick(e) {
  if(e.classList.contains('close'){
   //do stuff
  }else{
   //
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The element that you received from the HostListener / Event is the target which is an HTMLElemenet or in this case I think the HTMLButtonElement.

You can use the usually JavaScript methods for HTMLElements on them. In this case you are looking for e.classList.contains('close').

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