当角度 Js 为零时隐藏输入中的值 [英] Hide Value in input when it's zero in angular Js
本文介绍了当角度 Js 为零时隐藏输入中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个角度对象 item.add_value = 0 绑定到
I have an angular object item.add_value = 0 bind into the
<input type="number" ng-model="item.add_value" ng-change="sumUp(item)" min="0" max="10000000000" />
为了进行计算,我必须使用 type="number" 而不是 type="text"
In order to do the calculation I have to use type="number" not type="text"
那我的问题是当值为0时如何隐藏input的内容?
Then my question is how to hide the content of the input when the value is 0?
推荐答案
我刚刚遇到了同样的问题,并找到了一种方法来修复/改进 @dubadub 的答案,所以我要分享我的指令版本:
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;
});
}
};
})
您只需在输入中添加 hide-zero
属性即可使用它.
You just use it by adding the hide-zero
attribute to your inputs.
这篇关于当角度 Js 为零时隐藏输入中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文