检测键是否“删除”被angularJS按压 [英] Detect if key "delete" was pressed with angularJS
本文介绍了检测键是否“删除”被angularJS按压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我有以下代码:
angular.module( myApp,[]).controller(myController,function($ scope){$ scope.pressedKey = function(keyObj){$ scope.myKey = keyObj.key;}});
< script src =https://ajax.googleapis.com/ajax/libs /angularjs/1.2.23/angular.min.js\"></script><div ng-app =myAppng-controller =myController> < input ng-keypress =pressedKey($ event)>< br> {{myKey}}< / div>
我在输入上使用ng-keypress来检测是否存在按键事件和哪个按键被点击。我需要所有的数字和字母以及输入和删除键。现在,数字,字母和输入工作正常,但是当我点击删除键时,什么也没有发生。我该如何检测它也有角?
谢谢&
使用 ng-keydown
而不是 ng-keypress
:
angular。 module(myApp,[]).controller(myController,function($ scope){$ scope.pressedKey = function(keyObj){$ scope.myKey = keyObj.key;}}); $ c $ < script src =https://ajax.googleapis.com/ajax 您可以详细了解 keydown
和按键$之间的区别c $ c> 此处。
Hi I've got follow code:
angular.module("myApp", []).controller("myController", function($scope) {
$scope.pressedKey = function(keyObj) {
$scope.myKey = keyObj.key;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
<input ng-keypress="pressedKey($event)"><br>{{myKey}}
</div>
I use the ng-keypress on an input for detecting if there was a key-event and which key was clicked. I need all numbers and letters and also the enter and delete key. Now, the numbers, letters and the enter works fine, but when I click the delete key, nothing happens. How can I detect it also with angular?
Thanks & cheers.
解决方案 Use ng-keydown
instead of ng-keypress
:
angular.module("myApp", []).controller("myController", function($scope) {
$scope.pressedKey = function(keyObj) {
$scope.myKey = keyObj.key;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
<input ng-keydown="pressedKey($event)"><br>{{myKey}}
</div>
You can read more about the difference between keydown
and keypress
here.
这篇关于检测键是否“删除”被angularJS按压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文