我无法让 $formatters 函数像我想要的那样工作。我也找不到我在任何地方寻找的一个例子,所以我将发布我的答案以防有人需要它。
这是使用 ng-model 在输入框中格式化我们的货币的指令
.directive('uiCurrency', function ($filter, $parse) {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attrs, ngModel) {
function parse(viewValue, noRender) {
if (!viewValue)
return viewValue;
// strips all non digits leaving periods.
var clean = viewValue.toString().replace(/[^0-9.]+/g, '').replace(/\.{2,}/, '.');
// case for users entering multiple periods throughout the number
var dotSplit = clean.split('.');
if (dotSplit.length > 2) {
clean = dotSplit[0] + '.' + dotSplit[1].slice(0, 2);
} else if (dotSplit.length == 2) {
clean = dotSplit[0] + '.' + dotSplit[1].slice(0, 2);
}
if (!noRender)
ngModel.$render();
return clean;
}
ngModel.$parsers.unshift(parse);
ngModel.$render = function() {
console.log('viewValue', ngModel.$viewValue);
console.log('modelValue', ngModel.$modelValue);
var clean = parse(ngModel.$viewValue, true);
if (!clean)
return;
var currencyValue,
dotSplit = clean.split('.');
// todo: refactor, this is ugly
if (clean[clean.length-1] === '.') {
currencyValue = '$' + $filter('number')(parseFloat(clean)) + '.';
} else if (clean.indexOf('.') != -1 && dotSplit[dotSplit.length - 1].length == 1) {
currencyValue = '$' + $filter('number')(parseFloat(clean), 1);
} else if (clean.indexOf('.') != -1 && dotSplit[dotSplit.length - 1].length == 1) {
currencyValue = '$' + $filter('number')(parseFloat(clean), 2);
} else {
currencyValue = '$' + $filter('number')(parseFloat(clean));
}
element.val(currencyValue);
};
}
};
})