The manual explicitly states that it accepts a numeric expression and nothing else:
number_expression | Currency [: CurrencyCode [: symbolDisplay [: digitInfo]]]
The pipe is very simple and can be expanded and used instead CurrencyPipeto match expected behavior:
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(-(\d+))?)?$/;
@Pipe({name: 'currency'})
export class LooseCurrencyPipe extends CurrencyPipe {
transform(value: any, currencyCode: string, symbolDisplay: boolean, digits: string): string {
if (typeof value === 'number' || _NUMBER_FORMAT_REGEXP.test(value)) {
return super.transform(value, currencyCode, symbolDisplay, digits);
} else {
return value;
}
}
}
To create a new channel with a different name, it CurrencyPipecan be entered into a user channel:
@Pipe({name: 'looseCurrency'})
export class LooseCurrencyPipe implements PipeTransform {
constructor(private _currencyPipe: CurrencyPipe) {}
transform(value: any, currencyCode: string, symbolDisplay: boolean, digits: string): string {
if (typeof value === 'number' || _NUMBER_FORMAT_REGEXP.test(value)) {
return this._currencyPipe.transform(value, currencyCode, symbolDisplay, digits);
} else {
return value;
}
}
}
To CurrencyPipebe introduced via DI, it must be added to the module providers:
declarations: [LooseCurrencyPipe, ...],
providers: [CurrencyPipe, ...],
estus source
share