AngularJS Press Key Twice With Different Actions After Each Keypress

140 views Asked by At

I need help pressing the Enter key twice with a different action occurring after each keypress in AngularJS, for example:

  • Press Enter
  • One action happens
  • Press Enter again
  • A different action happens

I can get the first action working, just not the second. Here's my code:

  $scope.enterPressed = 0;
  $document.bind('keydown', function (evt) {
    if (evt.which === 13) {
      alert('Enter pressed');
      if ($scope.enterPressed === 0) {
        $scope.enterPressed++;
        document.getElementById("result").innerHTML = "Enter pressed once. $scope.enterPressed is " + $scope.enterPressed;
      } else if ($scope.enterPressed === 1) {
        e.preventDefault();
        document.getElementById("result").innerHTML = "Enter pressed twice. $scope.enterPressed i: " + $scope.enterPressed;
      }
    }
  });

Here's a Plunk.

How can I get the second action to occur?

2

There are 2 answers

0
mastilver On BEST ANSWER

You are using e.preventDefault(); but e is not defined

0
Sean.W On

It should be

 evt.preventDefault();