angularjs - 如何在 AngularJS 中使用按键事件?

标签 angularjs angularjs-directive angular-ui

我想捕获下面文本框中的回车键按下事件。为了更清楚地说明,我使用 ng-repeat 来填充 tbody。这是 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>

这是我的模块:

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

我正在使用资源来填充表,我的 Controller 代码是:

function Ajaxy($scope, $resource) {
//controller which has resource to populate the table 
}

最佳答案

您需要添加一个指令,如下所示:

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>

关于angularjs - 如何在 AngularJS 中使用按键事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17470790/

相关文章:

javascript - ui-router状态url问题

javascript - $watch ngModel 从内部指令使用隔离范围

javascript - 使用 Angular Bootstrap Accordion 将 id 添加到 ng-template

angularjs - 使用 ControllerAs 语法从指令 Controller 访问父属性并且没有 $scope 注入(inject)

angularjs - 自定义行为 - 切换类 - 弹出框 - Angular UI Bootstrap

javascript - 如何导入模块并以 Angular 显示在 View 上?

angularjs - 无法从 Angular UI 模式中的 id 获取元素

jQuery UI Datepicker 无法在 Angular JS UI Bootstrap 模式中工作

AngularJS:在指令中使用服务

angularjs - Selenium 点击事件不会触发 angularjs ng-click