docs(core): add new example for HostListener (#34228)

PR Close #34228
This commit is contained in:
Jithil P Ponnan 2019-12-04 16:18:42 +05:30 committed by Andrew Kushnir
parent d3069dbec6
commit 2f3d41f081
1 changed files with 23 additions and 0 deletions

View File

@ -872,6 +872,29 @@ export interface HostListener {
* template: '<button counting>Increment</button>', * template: '<button counting>Increment</button>',
* }) * })
* class App {} * class App {}
*
* ```
*
* The following example registers another DOM event handler that listens for key-press events.
* ``` ts
* import { HostListener, Component } from "@angular/core";
*
* @Component({
* selector: 'app',
* template: `<h1>Hello, you have pressed keys {{counter}} number of times!</h1> Press any key to
* increment the counter.
* <button (click)="resetCounter()">Reset Counter</button>`
* })
* class AppComponent {
* counter = 0;
* @HostListener('window:keydown', ['$event'])
* handleKeyDown(event: KeyboardEvent) {
* this.counter++;
* }
* resetCounter() {
* this.counter = 0;
* }
* }
* ``` * ```
* *
* @Annotation * @Annotation