我不得不写IE8一些代码。 我有一个NG重复创建填充表:
<input production-qty type="text" class="input-mini" maxlength="3" ng-model="day.qtyA" ui-event="{ blur : 'updateProduction(day)' }" ng-disabled="day.type=='H'">
IE8不会做类型=数字
我想一个指令,将忽略该输入字段没有数字键....即击键.... 0 - 9
我不想让用户类型ABC和污染的模型,然后告诉他们的价值是无效的。 我宁愿不让他们进入,这不是摆在首位有效的数据。
HTML:
<input production-qty type="text" maxlength="3" ng-model="qty1">
指示:
app.directive('productionQty', function() {
return {
require: 'ngModel',
link: function (scope, element, attr, ngModelCtrl) {
function fromUser(text) {
var transformedInput = text.replace(/[^0-9]/g, '');
console.log(transformedInput);
if(transformedInput !== text) {
ngModelCtrl.$setViewValue(transformedInput);
ngModelCtrl.$render();
}
return transformedInput; // or return Number(transformedInput)
}
ngModelCtrl.$parsers.push(fromUser);
}
};
});
Plunker
另请参见在输入滤波器的NG-模型 。 以上我的回答是仿照关闭pkozlowski.opensource的答案。
我看着NG-模式,但它并不过滤什么是在文本框中显示。 它设置$scope.qty1
到undefined
,但不需要的字符在文本框中可见。
HTML:
<input type="number" name="graduationYear" ng-model="gradYear" only-num>
指示:
directive('onlyNum', function() {
return function(scope, element, attrs) {
var keyCode = [8, 9, 37, 39, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 110];
element.bind("keydown", function(event) {
//console.log($.inArray(event.which,keyCode));
if ($.inArray(event.which, keyCode) === -1) {
scope.$apply(function() {
scope.$eval(attrs.onlyNum);
event.preventDefault();
});
event.preventDefault();
}
});
};
});
首先包含js文件的代码numericInput.js
指令: -
.directive('numeric', function() {
return function(scope, element, attrs) {
$(element[0]).numericInput({ allowFloat: true });
};
})
HTML: -
<input type="text" numeric />
DEMO 数字演示
不是指令,但我只使用:
控制器:
$scope.blockNonNumber = function (val, field){
$scope[field] = val.toString().replace(/[^0-9]/g, '');
}
HTML:
<input type="text" ng-model="price" ng-change="blockNonNumber(price, 'price')" pattern="[0-99]">
它不是指令,但可以在指令中被用作wellside