Skip to content

Instantly share code, notes, and snippets.

@raunak-r
Created September 20, 2021 06:52
Show Gist options
  • Save raunak-r/98477fe5efde379105e7135bb459dd52 to your computer and use it in GitHub Desktop.
Save raunak-r/98477fe5efde379105e7135bb459dd52 to your computer and use it in GitHub Desktop.
Pipe implementation in Angular

Pipe implementation in Angular

Requirements -

- pipes/ => A folder to store all the pipes.
- shared-pipes.module.ts => A module file just outside to declare and export all the pipes.
- app.module.ts => Global app module to import the sharedPipesModule. 
Note - sharedPipesModule can be imported in any other module to be visible in all the components. 
For ex. - If a component is imported in another sub-app-module, then sharedPipesModule will have to be imported in sub-app-module file also.

Usage

{{value | customFunction}}
import { SharedPipesModule } from './shared/pipes/shared-pipes.module';
@NgModule({
imports: [
...
SharedPipesModule,
...
]
...
})
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'customFunction',
pure: true
})
export class CustomFunctionPipe implements PipeTransform {
transform(val: any, args?: any): any {
console.log(val);
// write logic here.
return val;
}
}
import { NgModule } from '@angular/core';
import { CustomFunctionPipe } from './all-pipes/custom-function.pipe';
@NgModule({
imports: [],
declarations: [
CustomFunctionPipe,
],
exports: [
CustomFunctionPipe,
]
})
export class SharedPipesModule { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment