0
我的html页面上有一个输入框,将数据绑定到ng模型。我使用指令将输入字段限制为数字。我无法使用默认的HTML5限制,例如type =“number”。我遇到的问题是,如果输入非数字字符两次,例如按123,然后两次k,则k被添加到123,导致123k。但是,当我按另一个键时,k被指令删除。AngularJS将输入限制为数字不起作用
有人可以帮助我走出来修复该字母出现问题,如果您按相同的键两次
该指令使用:
angular.module('app').
directive('onlyDigits', function() {
return {
restrict: 'A',
require: '?ngModel',
link: function (scope, element, attrs, ngModel) {
if (!ngModel) return;
ngModel.$parsers.unshift(function (inputValue) {
var digits = inputValue.split('').filter(function (s) { return (!isNaN(s) && s != ' '); }).join('');
ngModel.$viewValue = digits;
ngModel.$render();
return digits;
});
}
};
});
使用'变种位数= inputValue.replace(/ [^ \ d] /克, '');'代替过滤器,以删除非整数。 – dfsq