Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
4 views
in Web Technology by (47.6k points)

I want to catch the enter keypress event on the textbox below. To make it more clear I am using a ng-repeat to populate the tbody. Here is the HTML:

<td>

<input type="number" id="closeqty{{$index}}" class="pagination-right closefield" data-ng-model="closeqtymodel" data-ng-change="change($index)" required placeholder="{{item.closeMeasure}}" />

</td>

This is my module:

angular.module('components', ['ngResource']);

I am using a resource to populate the table and my controller code is:

function Ajaxy($scope, $resource) { 

//controller which has resource to populate the table 

}

1 Answer

+1 vote
by (106k points)
edited by

For using a keypress event in AngularJS you need to add a directive, as follows:-

See the Javascript code below:-

app.directive('myEnter', function () {

return function (scope, element, attrs) {

  element.bind("keydown keypress", function (event) {  

            if(event.which === 13) { 

              scope.$apply(function (){

                 scope.$eval(attrs.myEnter);

     }); 

           event.preventDefault(); 

          } 

       });

    }; 

});

Looking for Angularjs material from basics! Refer to this video on Angularjs provided by Intellipaat:

HTML Code:

<div ng-app="" ng-controller="MainCtrl"> 

<input type="text" my-enter="doSomething()">

</div>

Browse Categories

...