点击AngularJS后如何检测按键

前端之家收集整理的这篇文章主要介绍了点击AngularJS后如何检测按键前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在web应用程序上使用angularjs,我需要弄清楚,如果我点击某些地方,我可以检测到像ctrl,shift或alt这样的键.

例如,使用jquery,我可以通过访问Click函数参数来做到这一点.

有没有一些开箱即用的方式来获取有关角度的信息?

看看这个美丽的东西关于 AngularJS key handling

所以键盘代码为Ctrl,Shift,alt会是

Ctrl – 17
Alt – 18
Shift – 16

Working Demo

HTML

<!DOCTYPE html>
<html>
<head>
  <script src="angular.js"></script>
  <script src="script.js"></script>
</head>

<body ng-app="mainModule">
  <div ng-controller="mainController">
    <label>Type something:
      <input type="text"
             ng-keydown="onKeyDown($event)"
             ng-keyup="onKeyUp($event)"
             ng-keypress="onKeyPress($event)" />
    </label><br />
    <strong>KEY DOWN RESULT:</strong> {{onKeyDownResult}}<br />
    <strong>KEY UP RESULT:</strong> {{onKeyUpResult}}<br />
    <strong>KEY PRESS RESULT:</strong> {{onKeyPressResult}}
  </div>
</body>
</html>

脚本

angular.module("mainModule",[])
  .controller("mainController",function ($scope)
  {
    // Initialization
    $scope.onKeyDownResult = "";
    $scope.onKeyUpResult = "";
    $scope.onKeyPressResult = "";

    // Utility functions

    var getKeyboardEventResult = function (keyEvent,keyEventDesc)
    {
      return keyEventDesc + " (keyCode: " + (window.event ? keyEvent.keyCode : keyEvent.which) + ")";
    };

    // Event handlers
    $scope.onKeyDown = function ($event) {
      $scope.onKeyDownResult = getKeyboardEventResult($event,"Key down");
    };

    $scope.onKeyUp = function ($event) {
      $scope.onKeyUpResult = getKeyboardEventResult($event,"Key up");
    };

    $scope.onKeyPress = function ($event) {
      $scope.onKeyPressResult = getKeyboardEventResult($event,"Key press");
    };
  });
原文链接:https://www.f2er.com/angularjs/143134.html

猜你在找的Angularjs相关文章