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.

1
  • angularJS has simple syntax to accomplish this. angular does not. it's best to just sort your array in javascript. Commented Jun 6, 2020 at 19:40

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

you can use in *ngFor, let last=last and *ngIf="!last"
Yes better suggestion to use rather than index @Eliseo . Updated answer related your suggestion
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.