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

156
Visualizações
Implementing a @hostListener only when component is called

Angular application that uses a ngx-datatable. This table auto populates its [rows]. Within these rows is an [action] that displays a popup of items. This popup is a seperate component away from the table. I am calling the popup from the table in a parent > child link e.g.

<ngx-datatable-column name="Actions">
<ng-template let-row="row" ngx-datatable-cell-template>
    <app-actions-pop-up [actions]="row.actions"></app-actions-pop-up>
</ng-template>
</ngx-datatable-column>

This calls the action popup component. I have an @HostListener that listens to the clicks on the page for an Event. This then returns true/false to open or close the popup.

This is costly as my application will have 1000's of rows and it will impact the rendering as a click is being listened too regardless of whether the actions button is being clicked on.

e.g. if there are 4 row items, it will loop through 4 times. Imagine if there were 1000s of rows.

There is a lovely solution found here: Detect click outside Angular component where by it adds a @Hostlistener() to the document click event only once inside the popup component. The event should push the value of the clicked target element inside a public subject stored in a global utility service.

However, I have struggled to implement this thus far in my setup. Any help would be appreciated here - stackBlitz example

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

0

I would suggest simply adding (document:click)="clickout($event)" on your popup container.

html

<div *ngIf="isActive" (document:click)="clickout($event)" class="actions__container">

ts

clickout(event) {
  this.isActive = this.eRef.nativeElement.contains(event.target);
}

Angular will subscribe to that event only in case if popup is open and unsubscribe when it's closed. So you will have zero or only one subscription.

You can also read one of my article regarding this optimization

  • Simple Angular context help component or how global event listener can affect your perfomance

Forked Stackblitz

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