我在尝试使用type =“number”解析输入字段时遇到了一些问题。
我的代码在这里:http://codepen.io/anon/pen/zBxpxr
功能
ngModel.$parsers.push(function(inputValue) {
插入+或 - 时不调用。但它适用于所有其他情况。
为什么?
答案 0 :(得分:0)
默认情况下,输入类型编号允许使用+, - 和e编号。
我建议你使用这个指令:
myapp.directive('allowPattern', function () {
return {
restrict: "A",
link: function (scope, element, attrs) {
element.bind("keypress", function(event) {
var keyCode = event.which || event.keyCode;
var keyCodeChar = String.fromCharCode(keyCode);
if (!keyCodeChar.match(new RegExp(attrs.allowPattern, "i"))) {
event.preventDefault();
return false;
}
});
}
};
});
http://plnkr.co/2WBFaBKGyTMAz6g9i1S9
希望它有所帮助。