2017-04-10 30 views
0

I can move up and down with the keyboard keys but i cannot get the enter event correct角JS事件進入調用元素上的功能TR

**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進入犯規被調用的。 我能做些什麼以便輸入可以工作。

+1

基本上,如果您在任何輸入字段中使用ng-enter,它都可以工作。但是你在中使用。否則,請嘗試關注該元素並按下回車鍵(可能會或可能會起作用) – Srigar

+0

也請查看您的指令的'restrict'選項。它不能'使用嚴格',它應該''A「'例如 – devnull69

+0

可能重複[如何在AngularJS中使用按鍵事件?](http://stackoverflow.com/questions/17470790/how-to-使用-a -presspress-event-in-angularjs) –

回答

0

嘗試這樣的事情

.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(); 
      } 
     }); 
    }; 
}); 

的限制選項可以省略。

編輯:您將有一個屬性添加到您的<tr>,使他們可以集中並接收鑰匙*事件:

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

與其他號碼取代0您更多的focussable <tr>

+0

不工作,我想在tr上使用它 –

+0

查看最新的編輯...至少你可以嘗試。通常'tr'元素並不意味着接收關鍵事件 – devnull69

+0

不行不通 –