I'm using https://github.com/lgalfaso/angular-dynamic-locale to change locale at runtime. This works great for all the currency symbols, etc. but I want to be able to convert the currency amount too.
I've written a filter:
angular.module('kitchenapp.filters')
.filter('convert', ['CURRENCIES', 'tmhDynamicLocale', '_', function (CURRENCIES, tmhDynamicLocale, _) {
var currentLocale = tmhDynamicLocale.get();
var currency = _.find(CURRENCIES, {code: currentLocale});
var conversionRate = currency.rate;
return function (value) {
return value * conversionRate;
};
}]);
and I'm applying it like so in the html:
<p>{{vm.testAmount | convert | currency:undefined:2 }}</p>
When I change locale, as I said, the currency symbol changes but the conversion filter is not run. If I put it upstream of the currency filter it will run, but that's then double processing to strip the currency symbol and dividers, etc. Is there any way to avoid this and get the convert filter to run too when the locale changes?
I believe the function returned by the filter is the one that is executed multiple times and the conversionRate value is already binded to the scope of the function.
Try this:
angular.module('kitchenapp.filters')
.filter('convert', ['CURRENCIES', 'tmhDynamicLocale', '_', function (CURRENCIES, tmhDynamicLocale, _) {
return function (value) {
var currentLocale = tmhDynamicLocale.get();
var currency = _.find(CURRENCIES, {code: currentLocale});
var conversionRate = currency.rate;
return value * conversionRate;
};
}]);