CSC360: button and Keyboard input in angular2 [5/7] |
Here is a starter component with a button and keyboard input:
01 |
import {Component} from 'angular2/core'; @Component({ host: { '(window:keypress)': 'keyHandler($event)' }, selector: 'my-app', template: ` <h2>{{title}} : {{x}}</h2> <button (click)="f()">press me</button> `, }) export class AppComponent { title = "hello"; x = 41; f() { this.x = this.x + 1; return this.x; } keyHandler(event) { switch (event.keyCode) { case 43: this.x = 4000; break; default: this.x = event.keyCode; break; } console.log(event, event.keyCode, event.keyIdentifier); } } |