Search code examples
javascriptangularjslocale

Locale in Angular for currency use


I am pretty new to Angular (1.6.4) and basically I need to change the currency symbols to all the values displayed in my page by setting up $locale to this Angular app.

Currently I have something like this:

{{myCtrl.getPrice() | currency : myCtrl.user.currency.symbol}}  //returns $10

I want to replace that line with this one:

{{myCtrl.getPrice() | currency}}

In order to use the currency set by $locale, but my question is how to set $locale ? The documentation online is very skinny on this issue.

I am grateful for any tip because I am pretty stuck right now, thanks !


Solution

  • All you need to do is to change NUMBER_FORMATS.CURRENCY_SYM constant of $locale service (remember to inject it in controller):

    .controller('MainCtrl', ['$locale', function ($locale) {
      // this.user should come from somewhere ...
    
      $locale.NUMBER_FORMATS.CURRENCY_SYM = this.user.currency.symbol
    }])
    

    If you need to set currency symbol for entire application, it makes sense to change CURRENCY_SYM in run block of the app.