Angular Standalone Pipe with Dependencies

Discussão em 'Angular' iniciado por Lycodo, Dezembro 17, 2024.

  1. Lycodo

    Lycodo Guest

    In Angular 18, is it possible to have a custom standalone pipe that uses another pipe?

    Here is what I am trying to do:

    import { inject, Pipe, PipeTransform } from '@angular/core';
    import { CurrencyPipe } from '@angular/common';

    @Pipe({
    name: 'currencyZeroBlank',
    standalone: true,
    })
    export class CurrencyZeroBlankPipe implements PipeTransform {
    private readonly currencyPipe = inject(CurrencyPipe);

    public transform(value: number | undefined): string {
    if (value === null || value === 0) {
    return '';
    }
    return this.currencyPipe.transform(value, 'EUR')!;
    }
    }


    But I get

    NullInjectorError: No provider for _CurrencyPipe!

    For a standalone component, this would be only a matter of

    imports: [CommonModule],


    but for a pipe this does not work. Can it be done, or do I need to add a module and declare it?

    Continue reading...

Compartilhe esta Página