Trying to inject the DecimalPipe into an Angular service

Discussão em 'Angular' iniciado por bob.mazzo, Setembro 3, 2025.

  1. bob.mazzo

    bob.mazzo Guest

    I understand how to use the DecimalPipe in an Angular HTML template, while importing it in the component code. However, I cannot figure out how to use the DecimalPipe in an Angular service. I keep getting the "No provider" error.

    import { DecimalPipe } from '@angular/common';
    import { inject, Injectable } from '@angular/core';

    @Injectable({
    providedIn: 'root',
    })
    export class CompareService {

    private readonly decimalPipe = inject(DecimalPipe);

    getRows() {

    const myValue = 249.19975;
    const cst = decimalPipe .transform(myValue, '1-0-0');

    // additional code here...
    }
    }
    }


    The error is:

    error.handler.ts:10 ɵNotFound: NG0201: No provider found for `DecimalPipe`. Source: Standalone[Compare..Component]. Path: CompareService -> DecimalPipe.


    I've tried to provide it to the parent component - i.e. providers: [DecimalPipe], - but no matter what I do, it keep throwing the error.

    Perhaps in the end I cannot use a pipe within a service?

    Continue reading...

Compartilhe esta Página