Please find the fiddle http://jsfiddle.net/q2SgJ/5/
WANTS: {{val | number:2}} in \"input\"
-
I expanded on the accepted answer to look at the step attribute when determining how many decimal places to limit.
directive('forcePrecision', function () {
return {
restrict: 'A',
scope: {
step: '@'
},
link: function (scope, element, attrs) {
if (!scope.step || scope.step == 'any') {
return;
}
var prec = 1;
for (var i = scope.step; i != 1; i *= 10) {
prec *= 10;
}
element.on('keypress', function (e) {
var val = Number(element.val() + (e.charCode !== 0 ? String.fromCharCode(e.charCode) : ''));
if (val) {
var newVal = Math.floor(val * prec) / prec;
if (val != newVal) {
e.preventDefault();
}
}
});
}
};
});