AngularJS backspace keypress

26,135

Got it!

<input type="text" ng-keydown="removeTagOnBackspace($event)" />

And:

$scope.removeTagOnBackspace = function (event) {
    if (event.keyCode === 8) {
        console.log('here!');
    }
};
Share:
26,135
Matheus Lima
Author by

Matheus Lima

Updated on July 09, 2022

Comments

  • Matheus Lima
    Matheus Lima almost 2 years

    I need to capture user's backspaces inside an input.

    So I've done this:

    <input type="text" ui-keypress="{8:'removeTagOnBackspace()'}" ng-model="searchStudent" />
    

    And then, inside my controller I've done this, just to check if it's working:

    $scope.removeTagOnBackspace = function() {
        console.log('here');
    };
    

    But is not printing anything. What is wrong with this? Is angular able to capture backspaces?

  • Iain Collins
    Iain Collins almost 9 years
    Thanks, this was interesting to run into - ng-keypress doesn't capture backspace key events (but does work for other keys), but ng-keydown works for the backspace key just fine.
  • jsruok
    jsruok over 5 years
    @mahesh Works with IE10
  • jsruok
    jsruok over 5 years
    Ended up using ng-keyup because when keydown was called the input was not yet updated.