Javascript Angular2 中 (keyup) 的选项是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32155887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:41:38  来源:igfitidea点击:

What are the options for (keyup) in Angular2?

javascripthtmlonkeyupangular

提问by AlikElzin-kilaka

The following works great when the enterkey is released. What other options are available for the keyupin addition to keyup.enter?

回车键被释放时,以下效果很好。除了?之外还有哪些其他选项可用?keyupkeyup.enter

<input #inputstring (keyup.enter)="doSomething(inputstring.value)"/>

回答by Angular University

These are the options currently documented in the tests: ctrl, shift, enter and escape. These are some valid examples of key bindings:

这些是当前在测试中记录的选项:ctrl、shift、enter 和 escape。这些是键绑定的一些有效示例:

keydown.control.shift.enter
keydown.control.esc

You can track this herewhile no official docs exist, but they should be out soon.

您可以在没有官方文档的情况下在此处进行跟踪,但它们应该很快就会发布。

回答by protalk

I was searching for a way to bind to multiple key events - specifically, Shift+Enter - but couldn't find any good resources online. But after logging the keydown binding

我正在寻找一种绑定到多个关键事件的方法 - 特别是 Shift+Enter - 但在网上找不到任何好的资源。但是在记录keydown绑定之后

<textarea (keydown)=onKeydownEvent($event)></textarea>

I discovered that the keyboard event provided all of the information I needed to detect Shift+Enter. Turns out that $eventreturns a fairly detailed KeyboardEvent.

我发现键盘事件提供了检测 Shift+Enter 所需的所有信息。结果是$event返回了一个相当详细的KeyboardEvent

onKeydownEvent(event: KeyboardEvent): void {
   if (event.keyCode === 13 && event.shiftKey) {
       // On 'Shift+Enter' do this...
   }
}

There also flags for the CtrlKey, AltKey, and MetaKey (i.e. Command key on Mac).

还有 CtrlKey、AltKey 和 MetaKey(即 Mac 上的 Command 键)的标志。

No need for the KeyEventsPlugin, JQuery, or a pure JS binding.

不需要 KeyEventsPlugin、JQuery 或纯 JS 绑定。

回答by Kasper

This file give you some more hints, for example, keydown.up doesn't work you need keydown.arrowup:

这个文件给了你更多的提示,例如,keydown.up 不起作用你需要 keydown.arrowup:

https://github.com/angular/angular/blob/630d93150a58581a0d474ebf1befb5d09b6813c5/modules/angular2/src/platform/browser/browser_adapter.dart

https://github.com/angular/angular/blob/630d93150a58581a0d474ebf1befb5d09b6813c5/modules/angular2/src/platform/browser/browser_adapter.dart

回答by Md Ayub Ali Sarker

you can add keyup event like this

你可以像这样添加keyup事件

template: `
  <input (keyup)="onKey($event)">
  <p>{{values}}</p>
`

in Component, code some like below

在组件中,代码如下

export class KeyUpComponent_v1 {
  values = '';

  onKey(event:any) { // without type info
    this.values += event.target.value + ' | ';
  }
}

回答by Manohar Reddy Poreddy

Have hit the same problem today.

今天遇到了同样的问题。

These are poorly documented, an open issue exist.

这些文件记录不充分,存在一个未解决的问题。

Some for keyup, like space:

一些用于keyup,例如空格:

<input (keyup.space)="doSomething()">
<input (keyup.spacebar)="doSomething()">  

Some for keydown
(may work for keyup too):

一些用于keydown
(也可以用于 keyup):

<input (keydown.enter)="...">
<input (keydown.a)="...">
<input (keydown.esc)="...">
<input (keydown.alt)="...">
<input (keydown.shift.esc)="...">
<input (keydown.shift.arrowdown)="...">
<input (keydown.f4)="...">

All above are from below links:

以上全部来自以下链接:

https://github.com/angular/angular/issues/18870
https://github.com/angular/angular/issues/8273
https://github.com/angular/angular/blob/master/packages/platform-browser/src/dom/events/key_events.ts
https://alligator.io/angular/binding-keyup-keydown-events/

https://github.com/angular/angular/issues/18870
https://github.com/angular/angular/issues/8273
https://github.com/angular/angular/blob/master/packages/platform-浏览器/src/dom/events/key_events.ts
https://alligator.io/angular/binding-keyup-keydown-events/

回答by Benny64

If your keyup event is outside the CTRL, SHIFT, ENTERand ESCbracket, just use @Md Ayub Ali Sarker's guide. The only keyup pseudo-event mentioned here in angular docs https://angular.io/docs/ts/latest/guide/user-input.htmlis ENTERkey. There are no keyup pseudo-events for number keys and alphabets yet.

如果您KEYUP事件外CTRLSHIFTENTERESC支架,只需用@Md阿尤布·阿里Sarker指南。在 angular 文档https://angular.io/docs/ts/latest/guide/user-input.html 中提到的唯一 keyup 伪事件是ENTER关键。目前还没有数字键和字母的 keyup 伪事件。