加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:59:46 所属栏目:安全 来源:网络整理
导读:我有一个角对象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时如何隐藏输入的内容?
我有一个角对象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;
            });
        }
    };
})

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读