2

So I'm struggling with this issue for some time now and yet I couldn't find an answer.

Basically I've this:

const formattedCurrency = $filter('currency')(input);

This returns me something like this $0.00 ($ seem to be the default angular curency CURRENCY_SYM:"$")

What I want to achieve is to make this return not $0.00but a dynamic currency, for example €0.00 or £0.00.

So let's assume that I have a html page with this text:

I have $0.00

If I do this:

this.$locale.NUMBER_FORMATS.CURRENCY_SYM = '€';

My page will instantly change into:

I have €0.00

This is great, but the problem is that I get the currency sign from a promise, like this:

this.getUserCurrency(this.id).then((currency) => {
    $locale.NUMBER_FORMATS.CURRENCY_SYM = currency; // this does not work
});

And the problem is that the above is not working, it doesn't change my HTML page even though my $locale was changed.

My conclusion after my investigations by now is:

If I change the $locale currency it will automatically change all my currencies from my page, but if I do this inside an asynchronous function (promise), my locale will get changed but it won't affect the DOM anymore.

Any solution to this problem?

4
  • Can you please try with function(error) { console.error(error) }) on your promise function Commented Nov 21, 2017 at 9:55
  • 1 sec i will reply. I was out but now I'm checking the answers Commented Nov 21, 2017 at 12:00
  • Any feedback m8? Commented Nov 29, 2017 at 19:51
  • Hello, I've given up on this since I have not received permissions to edit the child app, and since the answer did not gave me a solution for changing only the parent app. I upvoted your answer and probably many others will find it helpful and will upvote it also. Cheers Commented Nov 29, 2017 at 21:33

2 Answers 2

2

$filter in a AngularJS component & $locale are not parts of the digest cycle. In that way you are not be able to trigger a automatic $scope update by changing the $locale configuration object. In my opinion, its the best way to not use $locale as a part of your digest cycle (cause you are not able to do it that way).

You should persist your currency configuration in a global available variable or factory (which is a part of the digest cycle). You could use $rootScope to make your E2E binding work in a nice way. Also set your currency in $locale.NUMBER_FORMATS.CURRENCY_SYM to proceed with this configuration in other code parts.

View:

<div ng-controller="MyCtrl">
   {{ value | currency: $root.currencySymbol }}
</div> 

AngularJS application:

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope, $rootScope, $filter, $locale, $http, $timeout) {

   $scope.value = 0.00;
   $rootScope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM;

   $http.get('https://jsonplaceholder.typicode.com/posts').then(function (result) {
      $rootScope.currencySymbol = '€';
      $locale.NUMBER_FORMATS.CURRENCY_SYM = '€';
   });
});

>> Demo fiddle


Same approach by using a factory:

View

<div ng-controller="MyCtrl">
   {{ value | currency: $root.currencyHandler.symbol }}
</div>

AngularJS application

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function (
  $scope, 
  $rootScope, 
  $filter, 
  $locale, 
  $http, 
  $timeout,
  currencyHandler
  ) {

   $scope.value = 0.00;
   $rootScope.currencyHandler = currencyHandler;

   $http.get('https://jsonplaceholder.typicode.com/posts').then(function (result) {
      currencyHandler.setCurrencySymbol('€');
   });
});

myApp.factory('currencyHandler', function ($locale) {
    return {
    symbol: $locale.NUMBER_FORMATS.CURRENCY_SYM,
    setCurrencySymbol: function (value) {
      this.symbol = value;
        $locale.NUMBER_FORMATS.CURRENCY_SYM = value;
    }
  }
});

>> Demo fiddle


An other approach by using $watch inside a factory:

In that way you are able to set $locale.NUMBER_FORMATS.CURRENCY_SYM directly in your controller logic:

View

<div ng-controller="MyCtrl">
   {{ value | currency: $root.currencyHandler.symbol }}
</div>

AngularJS application

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function (
  $scope, 
  $rootScope, 
  $filter, 
  $locale, 
  $http, 
  $timeout,
  currencyHandler
  ) {

   $scope.value = 0.00;
   $rootScope.currencyHandler = currencyHandler;

   $http.get('https://jsonplaceholder.typicode.com/posts').then(function (result) {
      $locale.NUMBER_FORMATS.CURRENCY_SYM = '€'
   });
});

myApp.factory('currencyHandler', function ($locale, $rootScope) {

  let currencyHandler = {
    symbol: $locale.NUMBER_FORMATS.CURRENCY_SYM
  };

  $rootScope.$watch(function () {
    return $locale.NUMBER_FORMATS.CURRENCY_SYM;
  }, function (value) {
    currencyHandler.symbol = value;
  });

  return currencyHandler;
});

>> Demo fiddle

Sign up to request clarification or add additional context in comments.

10 Comments

The thing is that I have to deal with 2 applications, the parent and the child app. In the child app I have the curency. In the parent app I have the $locale. The child app is imported into the parent via npm package. I am constrained to edit only the parent. So I can't change the child, meaning that I don't have access to the templates to edit this {{ value | currency: $root.currencyHandler.symbol }}. Thats why I was forced to check a solution via $locale because If I change $locale in the parent app, the child app works without any change.
@paulalexandru I see. Sooo, there is no solution for this. You need to manually execute $filter('currency')(input); each time $locale.NUMBER_FORMATS.CURRENCY_SYM changes.
Ok man, thank you for your help. I will rate up your answer and I will try to see if I can get an approval to make changes on the child app also.
To be honest I thought threre was an existing solution with the $locale thing. Currently I try to get permissions to change the child app and if I get and I manage to implement the code and it works I will yes.
@paulalexandru allright, good. Next time please provide all information about your application. In that way we are able to create a full qualified answer for you. Missing information like "2 applications" - "unable to access templates" are important limitations.
|
0

You can force angularjs to refresh the $locale impact via $state.reload().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.