Angularjs keyboard event binding and angularjs keyboard binding
Bind Angularjs Keyboard Events
Recommended button
Method 1: ng built-in commands
<Button ng-click = "login ()" ng-keypress = "todoSomething ($ event)" class = "btn-success btn-lg" ng-disabled = "loginForm. $ invalid "> logon </button>
Note: bind a todoSomething method to $ scope in the corresponding controller.
$ Scope. todoSomething = function ($ event) {if ($ event. keyCode = 13) {// press ENTER login ();}}
Method 2: Custom commands
Html <button ng-click = "login ()" ng-enter = "login ()" class = "btn-success btn-lg" ng-disabled = "loginForm. $ invalid "> logon </button>
Command
myApp.directive('ngEnter', function () { return function (scope, element, attrs) { element.bind("keydown keypress", function (event) { if (event.which === 13) { scope.$apply(function () { scope.$eval(attrs.ngEnter); }); event.preventDefault(); } }); }; });
Conclusion: Both methods can achieve the carriage return login function. However, the recommended command method has low pollution to $ scope.
For more information about AngularJS command events, see: http://www.bkjia.com/article/119742.htm
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!