Create a directive may be?
myApp.directive("myNumber", function() {
return {
restrict: "EA",
template: "<input type='number' step='1' />",
replace: true,
link: function(scope, e, attrs) {
e.bind("keydown", function(e) {
if (!(e.keyCode >= 48 && e.keyCode <= 57)) {
e.preventDefault();
}
});
}
};
});
then use it in your app like so
<div ng-app="myApp">
<my-number><my-number>
</div>
You can garnish it further to allow bindings, and special keys
myApp.directive("myNumber", function() {
return {
restrict: "EA",
template: "<input type='text' ng-model='model' />",
replace: true,
scope: {model : "="},
link: function(scope, e, attrs) {
e.bind("keydown", function(e) {
// Special request from @endless for all these keys to be exempted
//
if (e.ctrlKey || e.altKey || e.keyCode >= 0 && e.keyCode <= 31 || e.keyCode >= 33 && e.keyCode <= 40 || e.keyCode==46) {
return;
}
else if (!(e.ctrlKey || (e.keyCode >= 48 && e.keyCode <= 57 && !e.shiftKey))) {
e.preventDefault();
}
}).bind("change", function() {
var e = angular.element(this);
if (isNaN(parseInt(e.val())) || e.val().indexOf(".") >= 0) {
alert("incorrect number value entered");
e.val("");
}
});
}
};
});
Here is a working plunkr example
Here is another fork of the above that allows all the special keys
Here is more info on directives