angularangular-pipeangular-providersangular-injector

Provide CustomPipe instead of CurrencyPipe


I want to provide CustomPipe instead of CurrencyPipe. Expected to import and use default CurrencyPipe, but it will use CustomPipe instead of it. I use this provider

{
  provide: CurrencyPipe,
  useClass: CustomPipe,
}

But app use default CurrencyPipe

Expecting to transform currency using CustomPipe, but using default CurrencyPipe import


Solution

  • When you define a pipe you indicate the "name". So you can use some like

    @Pipe({ name: 'currency', standalone: true }) //<--see the name is "currency"
    export class CustomPipe implements PipeTransform{
      transform(value: number): string {
        return `⭐️ ${value} ⭐️`;
      }
    }
    

    And it's not necessary use a provider. BTW check where are you using the provider. If your component it's standalone you need add when bootstrap also in imports

    @Component({
      selector: 'app-root',
      imports: [CustomPipe] //<--here
      ....
    })
    export class App{...}
    
    bootstrapApplication(App, { 
      providers: [{provide:CurrencyPipe,useClass:CustomPipe}] //<--and here
    });