user1352057
user1352057

Reputation: 3182

AngularJS - Dynamically change currency symbol on a Filter?

Question Background:

I have a drop-down that contains 4 different country options these being: UK (United Kingdom), US (United States), FR (France), GER (German). Depending on which drop-down value is selected I need to filter a price on my Controllers Scope object to show the correct currency symbol, for example:

If I select 'FR' in the checkbox, I would expect to see the '€' symbol on the filter:

enter image description here

If I would to select 'UK' i would expect to see the '£' appended and so on.

The Issue:

As stated above I have 4 different countries I can select and therefore I need to be able to dynamcially change the currency filter.

I have attempted to set this by a model property on the Scope but It hasn't worked so far.

The Code:

Currently I am using the standard AngularJS Currency filter, as shown:

       {{maxTextVal | currency : "€" : 2}}

maxTextVal is the value on the controllers Scope object. In the code above I have hard-coded the euro code (€) to produce the symbol, it is this filter value that I need to dynamically set.

Is it possible to change this value at run-time? Any help would be appreciated.

Upvotes: 2

Views: 4045

Answers (2)

AWolf
AWolf

Reputation: 8980

It's possible to change this at run-time but I'm not sure if there is an option at the currency filter directly.

Any way, you can write a custom filter that's using $sce.trustAsHtml(value) to correctly display the symbol. Or you could also inject the filter with currencyFilter to your scope and call that function and use $sce there.

Please have a look at the demo below or at this fiddle.

angular.module('demoApp', [])
	.filter('currencyWithLocale', function(currencyFilter, $sce) {
    	return function(input, locale) {
        	locale = locale || {currency: '$'};
        	return $sce.trustAsHtml(currencyFilter(input, locale.currency));
        }
    })
	.controller('mainCtrl', MainCtrl);
    
function MainCtrl($sce) {
	var vm = this;
    angular.extend(vm, {
    	total: 10.1,
        locales: [{
        	id:0,
            short: 'GER',
            currency: '€'
        }, {
        	id:1,
            short: 'FR',
            currency: '€'
        },{
        	id:2,
            short: 'UK',
            currency: '£'
        }]
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demoApp" ng-controller="mainCtrl as ctrl">
    <select ng-model="ctrl.selectedLocale" ng-options="locale as locale.short for locale in ctrl.locales">
    </select>
    <span ng-bind-html="ctrl.total | currencyWithLocale: ctrl.selectedLocale"></span> 
    <!--<br/>
    below is not working because it's not $sanitized.<br/>
    {{ctrl.total | currency: ctrl.selectedLocale.currency}}-->

</div>

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691765

Instead of passing the literal "€", pass a scope variable that contains the currently selected currency:

{{maxTextVal | currency : selectedCurrency : 2}}

Or, assuming you have a selected country, and that the country contains a currency:

{{maxTextVal | currency : selectedCountry.currency : 2}}

Upvotes: 2

Related Questions