Hide Value in input when it's zero in angular Js

后端 未结 7 2076
北恋
北恋 2020-12-19 15:56

I have an angular object item.add_value = 0 bind into the



        
7条回答
  •  青春惊慌失措
    2020-12-19 16:39

    I think, the most best approach is in using ngModelController because it is not uses DOM (we just in data layer, not in view, we need bind as usual except 0) and very strait-forward. It helps adjust binding between model and it's view in way we need:

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
        $scope.name = 'World';
    });
    
    app.directive('hideZero', function() {
        return {
            require: 'ngModel',
             link: function(scope, element, attrs, modelCtrl) {
    
               modelCtrl.$parsers.push(function (inputValue) {
    
                 if (inputValue === 0) {
                   return '';
                 }         
    
                 return inputValue;         
               });
             }
        }
    });
    

    See more https://docs.angularjs.org/api/ng/type/ngModel.NgModelController.

    Working plunker http://plnkr.co/edit/Zatou8lLx23xwXd1x9hd?p=preview.

提交回复
热议问题