我有一个角对象item.add_value = 0绑定到
<input type="number" ng-model="item.add_value" ng-change="sumUp(item)" min="0" max="10000000000" />
为了进行计算,我必须使用type =“number”而不是type =“text”
那我的问题是当值为0时如何隐藏输入的内容?
解决方法
我刚刚遇到同样的问题,并找到了一种方法来修复/改进@dubadub的答案,所以我正在分享他的指令版本:
.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; }); } }; })