我想在下面的文本框中捕获Enter键按下事件。为了更清楚地说明,我使用a ng-repeat来填充tbody。这是HTML:
ng-repeat
<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>
这是我的模块:
angular.module('components', ['ngResource']);
我正在使用一种资源来填充表,而我的控制器代码是:
function Ajaxy($scope, $resource) { //controller which has resource to populate the table }
您需要添加directive,如下所示:
directive
Javascript :
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(); } }); }; });
HTML :
<div ng-app="" ng-controller="MainCtrl"> <input type="text" my-enter="doSomething()"> </div>