AngularJS <input> with type set to 'number', how to force a initial display of value to a non-number string

落爺英雄遲暮 提交于 2021-02-10 14:51:39

问题


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'


回答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;
          }
        });
      }
    };
})


来源:https://stackoverflow.com/questions/31838057/angularjs-input-with-type-set-to-number-how-to-force-a-initial-display-of-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!