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'

4
  • I don't think so. I had a similar problem and my solution was to parse the string into an integer. Commented Aug 5, 2015 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 in order to visualise and recreate your problem. Commented Aug 5, 2015 at 16:45
  • What about using placeholder="-"? Commented Aug 5, 2015 at 16:45
  • This is a workaround. thx Commented Aug 6, 2015 at 16:37

1 Answer 1

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;
          }
        });
      }
    };
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.