代码之家  ›  专栏  ›  技术社区  ›  user1809790

AngularJS货币过滤器-欧元

  •  4
  • user1809790  · 技术社区  · 7 年前

    我正在使用AngularJS货币过滤器,并且在正确显示欧元符号时遇到问题。

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

    控制器:

    getPriceCurrency() {
        return `€ `;
    }
    

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

    谢谢

    4 回复  |  直到 7 年前
        1
  •  2
  •   Vivz    7 年前

    $sce 属于 ngSanitize € 直接指向欧元符号。

    var app = angular.module("app", ["ngSanitize"]);
     app.controller("myCtrl", function($scope, $filter,$sce) {
       var vm =this;
       vm.price=100;
       vm.getPriceCurrency=function() {
         return `€ `; // 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>
        2
  •  0
  •   Pritam Banerjee Ashish Karnavat    7 年前

    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>
        3
  •  0
  •   Vinod kumar G    7 年前

    使用此

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

    在控制器中,只需返回值

        getPriceCurrency() {
            return `8364; `;
        }
    
        4
  •  0
  •   user1809790    7 年前

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

    这将我的货币视为HTML并正确显示。