Reputation: 885
I am not able to return value in ng-keypress event. Idea is to prevent few characters being pressed. I Know I can do the same without returning true/false by event.preventdefault().
ng-keypress="return press($event,'text')" not working
scope.press = function(event)
{
event = (event) ? event : window.event;
var charCode = (event.which) ? event.which : event.keyCode;
if ((charCode > 31 && (charCode < 48 || charCode > 57)) || charCode==8 )
{
return;
}
else
{
return false;
}
}
Upvotes: 1
Views: 3310
Reputation: 594
try this directive
.directive('preventKey', function ($timeout, Ls) {
return {
restrict: 'A',
link: function ($scope, element, attrs, ngModel) {
element.bind('keydown', function (e, inputs) {
var code = e.keyCode || e.which;
var val = element[0].value;
var current = document.activeElement;
//Enter all Charcodes u want where u want to prevent keypress
if (code === 13) {
e.preventDefault();
}
})
}
}
}
In your Html you use
<input type="text" ng-model="something" preventKey>
Upvotes: 3