2017-09-01 131 views
4

欧元我使用AngularJS货币过滤器时遇到显示欧元正确签名的问题。AngularJS货币过滤器 -

的HTML:

{{ price.priceTotal | currency: myController.getPriceCurrency() }} 

控制器:

getPriceCurrency() { 
    return `€ `; 
} 

- 在上面的方法,我刚回国的欧元符号的代码,但通过这个返回的货币方法可以是任何,取决于所选货币。

我的问题是,货币符号不正确显示。它显示为€ 50例如,但我希望它显示为€ 50.我试图设置在getPriceCurrency方法直接在欧元符号€返回,然而这将最终被显示为??? (三个问号),一旦代码被部署。

是否有任何其他的解决方法,我可以做的就是欧元和其他货币符号正常显示?

感谢

+0

的可能的复制[如何获得特定的货币符号的角度JS,而不是默认的(在我的情况卢比符号)(美元$符号)(https://stackoverflow.com/questions/18612212/how-to -get-specific-currency-symbolrupee-symbol-in-case-in-angular-js-inste) – Vivz

+0

@Vivz我的问题不是如何显示货币符号。我知道我可以使用角度js过滤器。我的问题是货币符号显示不正确,这是不同的。 – user1809790

+0

检查下面的答案,看看它是否是你在找什么 – Vivz

回答

0

Try this(请参阅有关更多信息的官方文档):

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
</head> 
 
<body ng-app="currencyExample"> 
 
    <script> 
 
    angular.module('currencyExample', []) 
 
    .controller('ExampleController', ['$scope', function($scope) { 
 
     $scope.amount = 1234.56; 
 
    }]); 
 
</script> 
 
<div ng-controller="ExampleController"> 
 
    <input type="number" ng-model="amount" aria-label="amount"> <br> 
 
    default currency symbol ($): <span id="currency-default">{{amount | currency}}</span><br> 
 
    custom currency identifier (USD$): <span id="currency-custom">{{amount | currency:"USD$"}}</span><br> 
 
    no fractions (0): <span id="currency-no-fractions">{{amount | currency:"&euro;":0}}</span> 
 
</div> 
 
</body> 
 
</html>

+1

当我设置的货币:“&欧元”在工作正常的HTML。但这不是我所需要的。我需要从控制器获取货币,因为它是动态的。事情是,当我从控制器返回例如&欧元时,它就像那样显示,而不是合适的欧元符号。 – user1809790

0

使用此

{{ price.priceTotal | currency: myController.getPriceCurrency() | currency:"&euro;"}} 
    or 
{{ price.priceTotal | currency: myController.getPriceCurrency() | currency : "€"}} 

在控制器只返回值

getPriceCurrency() { 
     return `8364; `; 
    } 
+0

当我添加两个货币过滤器我什么都没得到,甚至没有价格。 – user1809790

2

您可以使用ngSanitize模块的$sce来执行此操作。这是为了确保html可以被信任并且防止任何易受攻击的XSS攻击。 Angular不会将字符串&#8364;直接转换为欧元符号。

var app = angular.module("app", ["ngSanitize"]); 
 
app.controller("myCtrl", function($scope, $filter,$sce) { 
 
    var vm =this; 
 
    vm.price=100; 
 
    vm.getPriceCurrency=function() { 
 
    return `&#8364; `; // return any currency 
 
} 
 
}); 
 
app.filter('unsafe', function($sce) { 
 
    return $sce.trustAsHtml; 
 
});
<!DOCTYPE html> 
 
<html ng-app="app"> 
 
<head> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.6.5/angular-sanitize.js"></script> 
 
</head> 
 
<body ng-controller="myCtrl as myController"> 
 
<!-- binding the currency to html --> 
 
    <div ng-bind-html="myController.price | currency: myController.getPriceCurrency()| unsafe"> 
 
    </div> 
 
</body> 
 
</html>

0

我设法通过NG绑定,HTML来解决它:

ng-bind-html="myController.price | currency: myController.getPriceCurrency()" 

这对待我的货币作为HTML并正确显示它。