Solvedangular Currency Pipe: Be able to set/change the default currency.
✔️Accepted Answer
The problem lies in this line:
var currency = currencyCode || 'USD';
When no currencyCode
is passed the fallback is simply a hardcoded string 'USD'. This could be changed so it is configurable just like done for locale.
The line above would become:
var currency = currencyCode || _currencyCode;
And the value can be injected in the constuctor like done for locale:
@Inject(CURRENCY_CODE) private _currencyCode: string
CURRENCY_CODE
could by default still be set to 'USD' but can be configured through a provider the same way like @agusdutra suggested in his answer.
providers: [
{ provide: CURRENCY_CODE, useValue: "EUR" },
],
I think this will make a lot of angular users very happy, there are an enormous amount of posts/issues on this topic.
Other Answers:
I found a solution to this. Create custom pipe that extends the original CurrencyPipe and give default parameter.
I actually override the default pipe:
/*
This pipe overrides the default currency pipe from angular,
this can be removed when angular supports currency defaults
https://github.com/angular/angular/issues/25461
*/
import { CurrencyPipe } from "@angular/common";
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({
name: "currency"
})
export class CurrencyNlProxyPipe extends CurrencyPipe implements PipeTransform {
constructor() {
super("nl");
}
transform(
value: any,
code = "EUR",
display = "symbol",
digitsInfo = "1.2-2",
locale = "nl"
) {
return super.transform(value, code, display, digitsInfo, locale);
}
}
You can also use tokens to configure the pipe in your module, but that was not necessary for my project.
Importing this pipe in your modules works, but I still 'expect' that overriding the defaults should be a feature, so I don't opt for closing this issue.
Yes, this is really necessary. Writing in every single template {{value | currency:'BRL'}} doesn't seem like a great solution.
I'm submitting a...
Current behavior
When using CurrencyPipe if you want to show a different currency than US Dollars, you have to set explicitly every time that you use the pipe in the templates.
<span> {{ 1200.18 | currency }} </span>
Output: $ 1,200.18
<span> {{ 1200.18 | currency:'GBP' }} </span>
Output: £1,200.18
Expected behavior
Be able to set in the root module, the default currency that you want to use using
provide: CURRENCY_ID
The currency symbol that is going to show by default when no currency is specified in the template, is the one defined in the loaded locale for that currency code.
For Example:
app.module.ts:
component.html:
If I'm using
LOCALE_ID = 'en'
Output: £1,200.18
If I'm using
LOCALE_ID = 'es'
Output: 1.200,18 GBP
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
I'm developing an application for a country where the Locale that we use, uses the symbol US$ for dollars.
So every time you use {{ 1200.18 | currency }} it outputs: US$ 1.200,18. And we don't want to use dollars.
We have a lot of currency outputs to display in our local currency (pesos, $) and found that expliciting it in every place we use, was not the best way to go.
We found a way of working this around, but for sure it's not the best option, I think this feature request would be a more suitable option.
What we did was change that 'US$' symbol for dollars:
That way, we could without expliciting in the templates the currency, show the code $ for currencies with the locale 'es-UY'
Others:
Issue associated with the request: #22519