Skip to content
Advertisement

How to print variable only once in ngFor?

and i need to print some “printDay” by condition only once, how can i do it?

  <div class="message" *ngFor="let message of messages">
  <div  *ngIf="lastMessageDate !== firstMessageDate " class="print-date" >{{printDay}}</div>
  <div [ngClass]="{'message__sent': message.messageType === 'sent'}" class="message__wrap message-item">
    <div class="message-item__data data">
      <span class="data__title">{{message.name}} {{message.DATE | date: 'HH:mm'}}</span>
      <div class="data__text">{{message.message}}</div>
      <div class="data__file-wrap file-wrap" *ngIf="message.send_diagnostic_data">
        <app-download-file (fileDownloadEvent)="downloadFile('log', message.UUID)"
                           [fileLabel]="'Отчет по логам'"></app-download-file>
        <app-download-file (fileDownloadEvent)="downloadFile('telemetry', message.UUID)"
                           [fileLabel]="'Отчет по телеметрии'"></app-download-file>
      </div>
    </div>
  </div>
</div>

enter image description here

Advertisement

Answer

If you want to print something only in the first child you can use first directive:

<div *ngFor="let message of messages; first as isFirst">
    <div *ngIf="isFirst"> {{ printSomething() }} </div>
</div>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement