Search Lessons, Code Snippets, and Videos
search by algolia
X
#native_cta# #native_desc# Sponsored by #native_company#

How to Reverse an Observable Array in Angular - AngularFire2 FirebaseListObservable

written by Jeff Delaney
full courses and content on fireship.io

It is ideal to unwrap observables in Angular inside the component HTML with the | async pipe. Sometimes you may want your list observable in reverse order, which you can achieve with a custom reverse pipe. This snippet was used to implement toast messages, showing the most recent first.

Gist

<div *ngFor="let item of listObservable | async | reverse">
{{item?.whatever}}
</div>
view raw gistfile1.txt hosted with ❤ by GitHub
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'reverse'
})
export class ReversePipe implements PipeTransform {
transform(value) {
if (!value) return;
return value.reverse();
}
}
view raw reverse.pipe.ts hosted with ❤ by GitHub