如何在 AngularJS 中使用按键事件? [英] How to use a keypress event in AngularJS?
本文介绍了如何在 AngularJS 中使用按键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在下面的文本框中捕捉回车键按下事件.为了更清楚,我使用 ng-repeat
来填充 tbody.这是 HTML:
这是我的模块:
angular.module('components', ['ngResource']);
我正在使用资源来填充表格,我的控制器代码是:
function Ajaxy($scope, $resource) {//具有填充表的资源的控制器}
解决方案你需要添加一个指令
,像这样:
Javascript:
app.directive('myEnter', function () {返回函数(范围、元素、属性){element.bind("keydown keypress", function (event) {if(event.which === 13) {范围.$应用(函数(){范围.$eval(attrs.myEnter);});event.preventDefault();}});};});
HTML:
<input type="text" my-enter="doSomething()">
I want to catch the enter key press event on the textbox below. To make it more clear I am using a ng-repeat
to populate the tbody. Here is the 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>
This is my module:
angular.module('components', ['ngResource']);
I am using a resource to populate the table and my controller code is:
function Ajaxy($scope, $resource) {
//controller which has resource to populate the table
}
解决方案 You need to add a directive
, like this:
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 中使用按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆