【问题标题】:keyup, keydown, blur are not working in angular 12keyup、keydown、模糊在角度 12 中不起作用
【发布时间】:2021-11-01 11:12:12
【问题描述】:

我正在使用 Angular 12 并尝试处理事件。唯一的点击事件正在工作,没有其他事件被触发。

allEvents.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-all-events',
  templateUrl: './all-events.component.html',
  styleUrls: ['./all-events.component.css']
})
export class AllEventsComponent implements OnInit {
  constructor() { }
  ngOnInit(): void {
  }
  myEvent(evt : any) {
    console.error(evt)
  };
 
}

allEvents.html


<button (click)="myEvent('I am click event')" >Click Event</button>
<br><br>
<input type="text" (onkey)="myEvent($event)" />

当我单击控制台上的按钮时,语句正在执行,但 keydown、keyup 或 blur 不起作用。

-----配置------

Angular CLI:12.2.12 节点:14.17.6 包管理器:npm 6.14.15 MACOS:达尔文 x64

【问题讨论】:

  • 你应该在 angular 输入元素上使用 (keyup), (keydown), (blur) 事件
  • onkey 是 javascript 事件吗?我找不到有关它的文档。
  • @Sanoj_V 随时分享有关它的文档。 Bilal,您应该阅读以下内容:angular.io/guide/user-input

标签: javascript angular angular-material


【解决方案1】:

此列表包含 Angular 支持的所有事件及其语法:

(click)="myFunction()"      
(dblclick)="myFunction()"   

(submit)="myFunction()"

(blur)="myFunction()"  
(focus)="myFunction()" 

(scroll)="myFunction()"

(cut)="myFunction()"
(copy)="myFunction()"
(paste)="myFunction()"

(keyup)="myFunction()"
(keypress)="myFunction()"
(keydown)="myFunction()"

(mouseup)="myFunction()"
(mousedown)="myFunction()"
(mouseenter)="myFunction()"

(drag)="myFunction()"
(drop)="myFunction()"
(dragover)="myFunction()"

如您所见,onkey 事件不存在,您应该使用keyupkeydownkeypress 来实现您的逻辑(猜你想使用的是这个)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    相关资源
    最近更新 更多