SoFunction
Updated on 2025-04-10

Angularjs keyboard event binding

Angularjs keyboard event binding

Recommended button

Method 1: ng built-in instructions

<button ng-click="login()" ng-keypress="todoSomething($event)" class="btn btn-success btn-lg" ng-disabled="loginForm.$invalid">
 Log in
</button>

Description: Bind a todoSomething method on $scope in the corresponding controller

 $=function($event){
   if($==13){//Enter     login();
   }
 }

Method 2: Customize the command

html
<button ng-click="login()" ng-enter="login()" class="btn btn-success btn-lg" ng-disabled="loginForm.$invalid">
 Log in
</button>

instruction

('ngEnter', function () {
   return function (scope, element, attrs) {
     ("keydown keypress", function (event) {
       if ( === 13) {
         scope.$apply(function () {
           scope.$eval();
         });
         ();
       }
     });
   };
 });

Summary: Both methods can realize the function of logging in by clicking the carriage, but the recommended command method has a relatively low pollution to $scope.

For AngularJS directive events, please refer to:https:///article/

Thank you for reading, I hope it can help you. Thank you for your support for this site!