AngularJS Catch Enter Keypress Event

Add Angular JS Dependency

First, we need to add Angular JS and in this tutorial, I'm using a CDN for Angular so you need internet connection for it to work. Place this inside the head tag of your html.
  1. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  2. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>

Define our App Name

Next, we need to define our app name using ng-app. You could place this in html, body or div tag depending on your needs. In this tutorial, I have placed it in html tag.
  1. <html ng-app="app">

Define our Controller

Same as app name you can place it in html, body or div tag using ng-controller but make sure that your app name comes first than your controller or your controller wont be read.
  1. <body ng-controller="myCtrl">

Creating our Textbox

Next step in to create our textbox where we assign our custom directive that we are going to create in our angular js named my-enter.
  1. <input type="text" class="form-control" ng-model="myinput" my-enter="alertInput()">

Creating our Angular JS Script

Lastly, we create our angular js script which contains our custom directive and our controller.
  1. var app = angular.module('app', []);
  2.  
  3. app.directive('myEnter', function () {
  4. return function ($scope, element, attrs) {
  5. element.bind("keydown keypress", function (event) {
  6. if(event.which === 13) {
  7. $scope.$apply(function (){
  8. $scope.$eval(attrs.myEnter);
  9. });
  10.  
  11. event.preventDefault();
  12. }
  13. });
  14. };
  15. });
  16.  
  17. app.controller('myCtrl', function($scope){
  18. $scope.alertInput = function(){
  19. alert($scope.myinput);
  20. }
  21. });
In here, we created a custom directive that upon Enter keypress, in our input html, alertInput function is called. In our controller, alertInput function alerts the input of our textbox. Here's the Full HTML of this tutorial:
  1. <!DOCTYPE html>
  2. <html ng-app="app">
  3. <meta charset="utf-8">
  4. <title>AngularJS Catch Enter Keypress Event</title>
  5. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  7. </head>
  8. <body ng-controller="myCtrl">
  9. <div class="container">
  10. <h1 class="page-header text-center">Press enter to alert Input Value</h1>
  11. <div class="col-sm-2 col-sm-offset-5">
  12. <input type="text" class="form-control" ng-model="myinput" my-enter="alertInput()">
  13. </div>
  14. </div>
  15. var app = angular.module('app', []);
  16.  
  17. app.directive('myEnter', function () {
  18. return function ($scope, element, attrs) {
  19. element.bind("keydown keypress", function (event) {
  20. if(event.which === 13) {
  21. $scope.$apply(function (){
  22. $scope.$eval(attrs.myEnter);
  23. });
  24.  
  25. event.preventDefault();
  26. }
  27. });
  28. };
  29. });
  30.  
  31. app.controller('myCtrl', function($scope){
  32. $scope.alertInput = function(){
  33. alert($scope.myinput);
  34. }
  35. });
  36. </body>
  37. </html>
That ends this tutorial. Happy Coding :)

Add new comment