Angular 2 How to Use ngif To run a function at the start or end of *ngFor loop Using *ngIf


Example

NgFor provides Some values that can be aliased to local variables

  • index -(variable) position of the current item in the iterable starting at 0
  • first -(boolean) true if the current item is the first item in the iterable
  • last -(boolean) true if the current item is the last item in the iterable
  • even -(boolean) true if the current index is an even number
  • odd -(boolean) true if the current index is an odd number
<div *ngFor="let note of csvdata; let i=index; let lastcall=last">
      <h3>{{i}}</h3> <-- to show index position
      <h3>{{note}}</h3>
      <span *ngIf="lastcall">{{anyfunction()}} </span><-- this lastcall boolean value will be true only if this is last in loop
      // anyfunction() will run at the end of loop same way we can do at start
    </div>