Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular using pipe from custom one

I would be glad to get some help importing a build-in pipe to a custom one in angular 2.

This is my code :

@Pipe({ name: 'tablePipe' })

export class TablePipe implements PipeTransform {
    constructor(private decimalPipe: DecimalPipe) {

    }
    transform(field: any, format: Format, formatArg: string): any {
        let formattedField: any = ''
        switch (format) {
            case 'number':
                {
                    formattedField = this.decimalPipe.transform(field, formatArg);
                    break;
                }
        }
        return formattedField;
    }
}

export type Format = 'date' | 'string' | 'number';

and this is the error i got:

EXCEPTION: Uncaught (in promise): Error: No provider for DecimalPipe!

When importing regular custom pipes in components, i am using :

@Component({
  ...,
  pipes: [MyCustomPipe],
  ...
})
like image 343
John Libes Avatar asked Mar 27 '17 13:03

John Libes


1 Answers

For this to work you need to add DecimalPipe to providers somewhere

For example

@NgModule({
  providers: [DecimalPipe],
  ...
})
export class AppModule {}

You can also add it to providers of a component (either the one where you 're using the pipe, or an ancestor component.

like image 134
Günter Zöchbauer Avatar answered Sep 19 '22 07:09

Günter Zöchbauer