I am using a Angular directive that formats input fields numbers with commas. I would also like this filter to automatically insert the decimal for cents.Every number will always have cents involved. For example if I wanted to put $1,000.00 in a input field i would have to type 100000. I made a plunkr but cant get the angular working
app.directive('format', ['$filter', function ($filter) {
return {
require: '?ngModel',
link: function (scope, elem, attrs, ctrl) {
if (!ctrl) return;
ctrl.$formatters.unshift(function (a) {
return $filter(attrs.format)(ctrl.$modelValue)
});
ctrl.$parsers.unshift(function (viewValue) {
var plainNumber = viewValue.replace(/[^\d|\-+|\.+]/g, '');
elem.val($filter('number')(plainNumber));
return plainNumber;
});
}
};
}]);