Created
May 27, 2020 09:15
-
-
Save yannickglt/c8062c2c0fb4ec72c9939d183a81b5b8 to your computer and use it in GitHub Desktop.
Generic pure Angular pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A safe way to call methods in templates | |
// Live demo @ https://stackblitz.com/edit/angular-generic-map-pipe | |
import { Pipe, PipeTransform } from "@angular/core"; | |
// Methods passed to the map pipe cannot (and should not) use the `this` keyword from components. Otherwise, it would not be a pure pipe. | |
@Pipe({ name: "map" }) | |
export class MapPipe implements PipeTransform { | |
public transform<T, R>( | |
thisArg: T, | |
project: (t: T, ...others: any[]) => R, | |
...args: any[] | |
): R { | |
return project(thisArg, ...args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment