<li *ngFor="#user of users "> {{ user.name }} is {{ user.age }} years old. </li>
Is it possible to invert the ngFor that the items are added bottom up?
You can simply use JavaScript's . reverse() on the array. Don't need an angular specific solution.
In *ngFor the * is a shorthand for using the new angular template syntax with a template tag, this is also called structural Directive.It is helpful to know that * is just a shorthand to explicitly defining the data bindings on a template tag.
JavaScript Array reverse()The reverse() method reverses the order of the elements in an array. The reverse() method overwrites the original array.
You can simply use JavaScript's .reverse()
on the array. Don't need an angular specific solution.
<li *ngFor="#user of users.slice().reverse() "> {{ user.name }} is {{ user.age }} years old. </li>
See more here: https://www.w3schools.com/jsref/jsref_reverse.asp
You need to implement a custom pipe that leverage the reverse method of JavaScript array:
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'reverse' }) export class ReversePipe implements PipeTransform { transform(value) { return value.slice().reverse(); } }
You can use it like that:
<li *ngFor="let user of users | reverse"> {{ user.name }} is {{ user.age }} years old. </li>
Don't forget to add the pipe in the pipes attribute of your component.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With