Angular 2 HostListener keypress detect escape key?

前端 未结 6 1118
温柔的废话
温柔的废话 2020-12-24 01:47

I am using the following method to detect keypresses on a page. My plan is to detect when the Escape key is pressed and run a method if so. For the moment I am just attempti

6条回答
  •  旧巷少年郎
    2020-12-24 02:08

    Angular makes this easy with the @HostListener decorator. This is a function decorator that accepts an event name as an argument. When that event gets fired on the host element it calls the associated function.

    @HostListener('document:keydown.escape', ['$event']) onKeydownHandler(event: 
        KeyboardEvent) {
        this.closeBlade();
       }
    

提交回复
热议问题