问题
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