I understand how to use the DecimalPipe in an Angular 20 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?