1

I am wondering using Angular, if the <input> has 'type' set to 'number', can you force a initial display of a non-number string if ng-model is initialized to certain number (0 in my example)?

My example code is here: http://plnkr.co/edit/TwraJlxZSd3TeSYscExq?p=info

This topic is related to: [1]: 'AngularJS directives to hide zero when <input> is initially loaded to the DOM'

and: [2]: 'Hide Value in input when it's zero in angular Js'

Community
  • 1
  • 1
WABBIT0111
  • 2,233
  • 2
  • 18
  • 30
  • I don't think so. I had a similar problem and my solution was to parse the string into an integer. – James Brierley Aug 05 '15 at 16:35
  • Please bring your code into the question itself, so people in the future, should "plnkr" die (or just temporarily fall over), can still see the relevant code, understand the question and learn from the answers. We only need the [minimal code](http://stackoverflow.com/help/mcve) in order to visualise and recreate your problem. – David Thomas Aug 05 '15 at 16:45
  • What about using `placeholder="-"`? – Danyel Cabello Aug 05 '15 at 16:45
  • This is a workaround. thx – WABBIT0111 Aug 06 '15 at 16:37

1 Answers1

1
The following directive will solve your problem,


app.directive('hideZero', function() {
    return {
      link: function(scope, element, attrs) {
        var modelName = attrs.ngModel;
        scope.$watch(modelName, function(newVal, oldVal){
          if(newVal === 0) {
            element[0].value = null;
            scope[modelName] = null;
          }
        });
      }
    };
})