Angularjs keyboard event binding and angularjs keyboard binding

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.