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

215
Visualizações
Angular 8+ event stoppropogation

I have a viewChild element which I show/hide by assigning a boolean value to it.

<div #acc class="user-info" [ngClass]="{'showDropdown': accountDropdownService.userDropdownIsVisible}">
  <div class="user-name">
    <div class="color-1 full-name">{{userdata.FirstName}} {{userdata.LastName}}</div>
    <div class="opened-eye-icon color-6__fm icon-icon_visible_off"
         [class]="balanceService.showBalance ? 'icon-icon_visible_off' : 'icon-icon_visible'"
         (click)="balanceService.toggleBalanceVisibility()"></div>
  </div>
  <div class="user-id color-6__fm">{{'General.Id'}}: <span><strong>{{userdata.Id}}</strong></span></div>
</div>

and its .ts file

@ViewChild('acc') public acc: ElementRef;

this.renderer.listen('window', 'click', (e: Event) => {
      if (e.target !== this.acc.nativeElement) {
        console.log('outside');
        console.log(e.target);
      }
    });

I need to hide viewChild element when I clicking outside of it, but when I click on its child elements like user-name or user-id its also understand as an outside click.

How can I prevent this when I'm clickin on div's children elements?

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

0

You want to make sure the target is not a descendant of the container, not just the container itself. You should use contains() instead.

this.renderer.listen('window', 'click', (e: Event) => {
  if (!this.acc.nativeElement.contains(e.target)) {
    console.log('outside');
    console.log(e.target);
  }
});

Otherwise, you would want to stop the propagation in the click event handlers for the children, that way this window listener won't see the event. But that might be more work for you than necessary.

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