5

I have basic ngFor directive for display data from an Array, And i want to show the last item of array at the top and last item of array at bottom of result?

I saw few Questions in here tell not to use pipes on ngFor. So how can i achieve this behavior in TypeScript.

Thanks in advance.

pl-jay
  • 970
  • 1
  • 16
  • 33
  • angularJS has simple syntax to accomplish this. angular does not. it's best to just sort your array in javascript. – Rick Jun 06 '20 at 19:40

2 Answers2

9

You can also use something in html like if only take last one to first. Updated answer thanks to @Eliseo' s nice approach.

<p *ngIf="list.length>0">{{list[list.length-1]}}</p>
<ng-container *ngFor="let item of list; let last=last">
   <p *ngIf="!last"> {{item}}</p>
</ng-container>
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
1

You should use sort function to your array in code. Or create a dublicate for origin array and sort it instead.

Anton Marinenko
  • 2,749
  • 1
  • 10
  • 16