加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 09:37:02 所属栏目:安全 来源:网络整理
导读:我想在下面的文本框中捕捉回车键按下事件。为了使它更清楚我使用ng重复填充tbody。这里是HTML: tdinput type="number" id="closeqty{{$index}}" class="pagination-right closefield" data-ng-model="closeqtymodel" data-ng-change="change($index)" requi
我想在下面的文本框中捕捉回车键按下事件。为了使它更清楚我使用ng重复填充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']);

我使用一个资源来填充表,我的控制器代码是:

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读