I've a custom decimal format pipe that uses angular Decimal pipe inturn. This pipe is a part of the shared module. I'm use this in feature module and getting no provider error when running the application.
Please ignore if there are any typo.
./src/pipes/custom.pipe.ts
import { DecimalPipe } from '@angular/common'; .. @Pipe({ name: 'customDecimalPipe' }) ... export class CustomPipe { constructor(public decimalPipe: DecimalPipe) {} transform(value: any, format: any) { ... }
./modules/shared.module.ts
import { CustomPipe } from '../pipes/custom.pipe'; ... @NgModule({ imports: [ .. ], declarations: [ CustomPipe ], exports: [ CustomPipe ] }) export class SharedModule { }
I inject the custom pipe in one of the components and call transform method to get the transformed values. The shared module is imported in he feature module.
If you want to use pipe's transform()
method in component, you also need to add CustomPipe
to module's providers:
import { CustomPipe } from '../pipes/custom.pipe'; ... @NgModule({ imports: [ .. ], declarations: [ CustomPipe ], exports: [ CustomPipe ], providers: [ CustomPipe ] }) export class SharedModule { }
Apart from adding the CustomPipe
to the module's provider list, an alternative is to add to the component's providers. This can be helpful if your custom pipe is used in only a few components.
import { CustomPipe } from '../pipes/custom.pipe'; ... @Component({ templateUrl: './some.component.html', ... providers: [CustomPipe] }) export class SomeComponent{ ... }
Hope this helps.
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