I just had this same problem and found a way to fix/improve on @dubadub's answer, so I'm sharing my version of his directive:
.directive('hideZero', function() {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attrs, ngModel) {
ngModel.$formatters.push(function (inputValue) {
if (inputValue == 0) {
return '';
}
return inputValue;
});
ngModel.$parsers.push(function (inputValue) {
if (inputValue == 0) {
ngModel.$setViewValue('');
ngModel.$render();
}
return inputValue;
});
}
};
})
You just use it by adding the hide-zero
attribute to your inputs.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…