0

I can move up and down with the keyboard keys but i cannot get the enter event correct

**Controller**

$scope.myFunction = function()
{
    alert('sadsad');
    alert('hi');
}

**Also i added directive like this** 

.directive('ngEnter', function () 
    {
        restrict:'use strict';
        return {
           link: function (scope, elements, attrs) {
              elements.bind('keydown keypress', function (event) {
                  if (event.which === 13) {
                      scope.$apply(function () {
                          scope.$eval(attrs.ngEnter);
                      });
                      event.preventDefault();
                  }
              });
           }
        };
    });

  **Below is the view code on which i m calling directive and function**

<tr  class="find"ng-repeat="busServices in  " ng-click="setSelected(busServices.bus_travel_id,this.busServices,$event)"  ng-class="{selectedsd:busServices.bus_travel_id === idSelected}" ng-mouseover="ShowHideBoarding($event,this.busServices,true)" ng-mouseleave="ShowHideBoarding($event,false)" ng-init="($first) ? setSelected(busServices.bus_travel_id,this.busServices) : ''" ng-enter="myFunction()"></tr>

ng-enter doesnt get called at all. what can i do so that ng enter can work.

1 Answers1

0

Try something like this

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

                event.preventDefault();
            }
        });
    };
});

The restrict option can be omitted.

EDIT: You will have to add an attribute to your <tr> so that they can be focussed and receive key* events:

<tr tabindex="0" ....>

Replace 0 with other numbers for more of your focussable <tr>

devnull69
  • 16,402
  • 8
  • 50
  • 61
  • See latest edit ... at least you can try. Normally `tr` elements are not meant to receive key* events – devnull69 Apr 10 '17 at 12:43
  • any alternative in javascript? – mittal chande Apr 10 '17 at 12:56
  • Then you are out of options. It means that your table row cannot accept the keypress event. What is your idea behind this concept? How do you want to make sure that one of your table rows is the "current element" you want to bind the `enter` key to? – devnull69 Apr 10 '17 at 12:59
  • In the table the rows are bus services when user hits enters on of of those bus services a function is called on hit if enter to load the seat layout on the basis of data present in the selected rows – mittal chande Apr 10 '17 at 13:02
  • You are talking about "hit enter on a row" and "selected row" ... what EXACTLY do you mean? How does your web app make one of the rows the selected one? – devnull69 Apr 10 '17 at 13:05
  • u can go down and up on the table rows . select one row , it becomes green when selected (as shown in the image) once u hit enter it will show seat layout of the bus (where the loading gif is shown in the image attached) – mittal chande Apr 10 '17 at 13:09
  • So your table row selection is a logical one (rather than a technical one, like the browser element focus). So you could use the keypress event on the document (e.g. the body element) and logically check which element is the current selected one. Keypress event on document should work – devnull69 Apr 10 '17 at 13:12