2016-12-29 327 views
0

大家好,这里是我关于自定义货币问题的短简单地说,我期待的欧洲货币格式,就像为:角自定义过滤器

Input : 123456789.22

Output : 123 456 789,22

我寻找实施到我的自定义过滤器的逻辑。

这是我的自定义过滤器和代码格式。

myApp.filter('myCurrency', function() { 
     return function(nStr) { 
     if (nStr) { 
      // return European currency format 
     }  
     } 
}) 

回答

0

注:发布他人分享知识。

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

 

 
myApp.filter('myCurrency', function() { 
 
    return function(nStr) { 
 
    if (nStr) { 
 
     nStr += ''; 
 
     x = nStr.split('.'); 
 
     x1 = x[0]; 
 
     x2 = x.length > 1 ? ',' + x[1] : ''; 
 
     var rgx = /(\d+)(\d{3})/; 
 
     while (rgx.test(x1)) { 
 
     x1 = x1.replace(rgx, '$1' + ' ' + '$2'); 
 
     } 
 
     return x1 + x2; 
 
    }  
 
    } 
 
}) 
 

 

 
function MyCtrl($scope) { 
 
    $scope.name = ''; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="MyCtrl"> 
 
    <input type="text" ng-model="name" /> You typed : {{name | myCurrency}}! 
 
</div>

+0

为什么不使用过滤器数量:2 ..它工作确定为我。 .. somthing like:€ –

+0

因为我想要的问题就像问题中提供的自定义格式,格式为三位数后的空格和逗号之间的小数点。 – Jigar7521

0

遵循这样的: http://www.encodedna.com/angularjs/tutorial/format-text-using-angularjs-currency-filter.htm

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script> 
 
</head> 
 
<body style="font:15px Verdana;"> 
 
    <div ng-app> 
 
     <p> 
 
      <label>Enter a number</label> 
 
      <input type="text" ng-model="bid" /> 
 
     </p> 
 

 
     <p> {{ bid | currency : "€"}} </p> 
 
    </div> 
 
</body> 
 
</html>

+0

谢谢,我已经知道了,这根据问题并不确切,如果我输入'111111111.22'答案应该是'111 111 111,22' – Jigar7521