Angular using pipe from custom one
Asked Answered
L

1

8

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],
  ...
})
Lyns answered 27/3, 2017 at 13:55 Comment(0)
D
16

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.

Donielle answered 27/3, 2017 at 13:57 Comment(1)
Glad to hear :) Please consider accepting my answer to indicate your problem is solved (clicking the white check mark below the up-down-vote button - thanks.Laplante

© 2022 - 2024 — McMap. All rights reserved.