Detect if key “delete” was pressed with angularJS

限于喜欢 提交于 2019-12-08 07:39:27

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/38220874/detect-if-key-delete-was-pressed-with-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!