Angular : only number type directive
Get $index within directive
by manoj
HTML
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/angular.js"></script>
<div ng-controller="MyCtrl">
Type anything : <br> <br>
<input type="number" only-num />
</div>
JavaScript
angular.module('myApp', [])
.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();
}
});
};
})
function MyCtrl($scope, $window) {
}