2016-12-29 261 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