当角度Js为零时,隐藏输入中的值

Ric*_*icc 4 angularjs

我有一个角对象item.add_value = 0绑定到

<input type="number" ng-model="item.add_value"   ng-change="sumUp(item)" min="0" max="10000000000" />
Run Code Online (Sandbox Code Playgroud)

为了进行计算,我必须使用type ="number"而不是type ="text"

那我的问题是当值为0时如何隐藏输入的内容?

小智 7

我刚刚遇到同样的问题,并找到了一种方法来修复/改进@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;
            });
        }
    };
})
Run Code Online (Sandbox Code Playgroud)

您只需将hide-zero属性添加到输入中即可使用它.