Skip to content

Instantly share code, notes, and snippets.

@konstantindenerz
Last active April 4, 2022 10:14
Show Gist options
  • Save konstantindenerz/dc8e1482f1f017cd8fd6f4bc43dc1edc to your computer and use it in GitHub Desktop.
Save konstantindenerz/dc8e1482f1f017cd8fd6f4bc43dc1edc to your computer and use it in GitHub Desktop.
Use this pipe to provide default values for null values of async pipe. `foo$ | async | nullTo:false`
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'nullTo',
})
export class NullToPipe implements PipeTransform {
/**
* Returns {@link value} or {@link newNullValue} if {@link value} is null.
*/
transform<T>(value: T | null, newNullValue: T): T {
return value !== null ? value : newNullValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment