Search code examples
angularangular-moduleangular-pipe

No Provider for CustomPipe - angular 4


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.


Solution

  • 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 { }