To convert currency from US to UK in AngularJS

后端 未结 2 723
予麋鹿
予麋鹿 2021-01-20 07:15

I tried this code to display but I need AngularJS to automatically convert currency:

 
default currency symbol ($): {{0.
相关标签:
2条回答
  • 2021-01-20 07:48

    AngularJs currencyFilter just formats output. If you want actually convert currency, you need to make custom filter, for example.

    Here is possible example:

    angular.module('myFilter', []).filter('currencyConverter', [function() {
       function convert(inputValue, currecyId) {
           // Your conversion code goes here
       }
    
       return function(inputValue, currencyId) {
          return convert(inputValue, currencyId);
       }
    });
    
    0 讨论(0)
  • 2021-01-20 08:05

    As @Andrey said, you should build your own custom filter to handle the currency conversion.
    Here's a simple demo of how I would build such a thing:

    angular.module('myModule').filter('currency', function() {
        var defaultCurrency = '$';
    
        return function(input, currencySymbol) {
            var out = "";
            currencySymbol = currencySymbol || defaultCurrency;
    
                switch(currencySymbol) {
                    case '£':
                        out = 0.609273137 * input; // google
                        break;
    
                    default: 
                        out = input;
                }
    
            return out + ' ' + currencySymbol;
        }
    });
    

    check the online demo

    0 讨论(0)
提交回复
热议问题