To convert currency from US to UK in AngularJS

后端 未结 2 724
予麋鹿
予麋鹿 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 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

提交回复
热议问题