3

I have an application that deals with different currencies. I get currency symbol from a web service and store that symbol in the controller's $scope.

$scope.symbol = '€';

When I try to show that in html,

This is working:

{{ 1500 | currency:"€" }}

This is not working

{{ 1500 | currency:symbol }}

here's a plunker. Any ideas?

1

5 Answers 5

6

If you want to bind html or markup you need to use "ng-bind-html" and mark the content as trusted on your controller. I'm unaware of any way of how to do this with the mustache binding mechanism. But this is the approach we've been using when needing to bind markup.

  1. Make the code trusted in the controller
  2. Wrap the filter in a custom filter - Limitation is that you'll still need ng-bind-html

Below are 3 options available to you:

Controller

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

app.controller('MainCtrl', function($scope,$sce) {      
    $scope.nonTrustedSymbol = '€';      
    $scope.trustedSymbol = $sce.trustAsHtml('€');      
})

.filter('currencyWithNumberFilter', ['$filter','$sce', 
    function ($filter, $sce) {
        return function (input, curr) {              
            var formattedValue = $filter('number')(input, 2);              
            return $sce.trustAsHtml(curr + formattedValue);
        }
    }]
 )

.filter('currencyWithCurrencyFilter', ['$filter','$sce', 
    function ($filter, $sce) {
        return function (input, curr) {              
            var formattedValue = $filter('currency')(input,curr);
            return $sce.trustAsHtml(formattedValue);
        }
    }]
 );

Markup

 <body ng-controller="MainCtrl">

      "Vanilla" controller & number filter:
      <span ng-bind-html=trustedSymbol></span>{{ 1500 | number:2 }}

      <br/>

      Custom filter, internally making use of Number filter for formatting:
      <span ng-bind-html="1500 | currencyWithNumberFilter:nonTrustedSymbol"></span>

      <br/>

      Custom filter, internally making use of Currency filter for formatting:
      <span ng-bind-html="1500 | currencyWithCurrencyFilter:nonTrustedSymbol"></span>

  </body>

Working sample

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

app.controller('MainCtrl', function($scope,$sce) {
  
  $scope.nonTrustedSymbol = '€';
  
  $scope.trustedSymbol = $sce.trustAsHtml('€');
  
})


  .filter('currencyWithNumberFilter', ['$filter','$sce', 
    function ($filter, $sce) {
        return function (input, curr) {
          
            var formattedValue = $filter('number')(input, 2);
          
            return $sce.trustAsHtml(curr + formattedValue);
        }
    }]
  )
  
  .filter('currencyWithCurrencyFilter', ['$filter','$sce', 
    function ($filter, $sce) {
        return function (input, curr) {
          
            var formattedValue = $filter('currency')(input,curr);
            return $sce.trustAsHtml(formattedValue);
        }
    }]
  );
<!DOCTYPE html>
<html ng-app="app">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
</head>
<body ng-controller="MainCtrl">  
  
  "Vanilla" controller & number filter:
  <span ng-bind-html=trustedSymbol></span>{{ 1500 | number:2 }}
  
  <br/>
  
  Custom filter, internally making use of Number filter for formatting:
  <span ng-bind-html="1500 | currencyWithNumberFilter:nonTrustedSymbol"></span>
  
  <br/>
  
  Custom filter, internally making use of Currency filter for formatting:
  <span ng-bind-html="1500 | currencyWithCurrencyFilter:nonTrustedSymbol"></span>

</body>

</html>

2
  • This works but the number won't be formatted if we do not use currency filter, i.e., 1500 instead of 1,500.00 Sep 15, 2014 at 7:35
  • 1
    updated the answer & fiddle. you'll need to use the number filter in addition to the ng-bind-html Sep 15, 2014 at 7:36
1

to an addition to Rohan's answer, where ng-bind-html was the solution, you could also use isoCurrency module.

if you have the ISO 4217 currency code (3 chars length e.g. USD, EUR, etc) isoCurrency can output the right format, fraction size and symbol.

// in controller
$scope.amount = 50.50;
$scope.currency = 'USD';

// in template
{{ amount | isoCurrency:currency }} // $50.50
0

Try this in your controller:

$scope.symbol = '€';

Plunker

2
  • I get &euro; symbol from my web service, so any ideas with that? Sep 15, 2014 at 7:05
  • Is it not possible for your webservice to simply return the symbols directly instead of returning the html entities of that symbols? If your service fetches data from db, you can simply store '€' inside your db just like normal string data. If you donot have access to webservice or db, this post might help you: stackoverflow.com/questions/21919533/… Sep 15, 2014 at 7:23
0

If using JQuery (Otherwise do the same thing but in pure javascript, it will take much more code but is possible, basically create a html dom element and insert your input as innerHTML to it, then read it's innerText)

function unescapeHTML(html) {
  return $("<div />").html(html).text();
}

And then of course

$scope.price=unescapeHTML('&euro;');
-1

To display currency symbol in angular js you need provide HTML entity for currency symbols below are the examples and usage in through code and in template :

Inside your Template example of Euro:

Item Price<span style="font-weight:bold;">{{price | currency:"&euro;"}}</span>

example of Rupee:

Item Price<span style="font-weight:bold;">{{price | currency:"&#8377;"}}</span>

Also check below url

http://www.cs.tut.fi/~jkorpela/html/euro.html

From controller :

Inject $filter in your controller

$scope.price=$filter('currency')($scope.price,'&euro;')
1
  • @Raghav try above solution. In my office plunker URl is restricted, kindly check and comment as final solution.
    – Vikas Garg
    Sep 15, 2014 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.