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

175
Visualizações
Angular: How to bind directives to elements with *ngFor

I have an <ng-container *ngFor='let column of columns'></ng-container> which displays multiple times on the page because of the *ngFor. I want to apply the "sticky" attribute (directive) to only the first of these ng-container elements. Not to the others. How can I do that? The output then should look like this:

<ng-container sticky></ng-container>
<ng-container></ng-container>
<ng-container></ng-container>
<ng-container></ng-container>
...
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The ngFor directive has a first variable that is true only for the first iteration.

ngIf has an else keyword that, in conjunction with a template variable can conditionally render one of two templates.

<ng-container *ngFor="let column of columns; first as isFirst">
    <ng-container *ngIf="isFirst; else notFirst" sticky></ng-container>
    <ng-template #notFirst></ng-template>
</ng-container>
about 4 years ago · Juan Pablo Isaza Relatório

0

<ng-container> will not exists in the DOM. It is used only to loop through the items and generate some content dynamically. So if you apply sticky directive to it directly, it will not have any effect. But you can apply sticky directive to elements that are generated by <ng-container>. I suggest you do add one <div> inside <ng-container> as a wrapper to the content and apply sticky directive to it. Also, you can use first variable of *ngFor to apply sticky directive only to the first element.

<ng-container *ngFor='let column of columns; let first as first'>
  <div *ngIf="first" sticky>
    <!--Your actual content-->
  <div>
  <div *ngIf="!first">
    <!--Your actual content-->
  <div>
</ng-container>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use index for this:

<ng-container *ngFor="let column of columns; let i = index">
    <div *ngIf="i=0" sticky></div>
    <div *ngIf="i!=0"></div>
</ng-container>
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