angularjs中输入字段的自定义货币过滤器
如何使用angularjs将值格式化为印度货币类型。 例如,454565到454565.00 我有如下输入字段:angularjs中输入字段的自定义货币过滤器,angularjs,Angularjs,如何使用angularjs将值格式化为印度货币类型。 例如,454565到454565.00 我有如下输入字段: <input type="text" ng-model="item.cost /> 您可以按如下方式为其创建指令 HTML 链接将价格定制为什么?提供更多有关exmaple将值从454565转换为454565.00的信息 function FormatMyNumber(yourNumber) { // Limit to two decimal places
<input type="text" ng-model="item.cost />
您可以按如下方式为其创建指令 HTML
链接将价格定制为什么?提供更多有关exmaple将值从454565转换为454565.00的信息
function FormatMyNumber(yourNumber) {
// Limit to two decimal places
yourNumber = parseFloat(yourNumber).toFixed(2);
//Seperates the components of the number
var n = yourNumber.toString().split(".");
//Comma-fies the first part
n[0] = n[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
//Combines the two sections
return n.join(".");
}
FormatMyNumber(454565); //yields 454,565.00
<div ng-app="myApp">
<div ng-controller="myCtrl">
{{amount}}
<input format-to-currency amount="amount">
</div>
</div>
angular.module('myApp', [])
.controller('myCtrl', function($scope) {
$scope.ampunt = 2;
})
.directive('formatToCurrency', function($filter) {
return {
scope: {
amount: '='
},
link: function(scope, el, attrs) {
el.val($filter('currency')(scope.amount));
el.bind('focus', function() {
el.val(scope.amount);
});
el.bind('input', function() {
scope.amount = el.val();
scope.$apply();
});
el.bind('blur', function() {
el.val($filter('currency')(scope.amount));
});
}
}
});