Angular 2 ion handling events 2 on keypress
I have a bunch of ionic 2-cards that I want to flip over with a key press (any key, it doesn't matter). The code looks like
<ion-content padding>
<ion-card (click)="setTime(7)" *ngIf="status == 'morning'" (keypress)="eventHandler($event)" style="width:80%">
<img src="https://greatist.com/sites/default/files/Sleeping-Positions-feature.jpg"/>
</ion-card>
</ion-content>
ts code
eventHandler(keyCode){
alert('hey vikj');
}
When any key is pressed, my event handler is not fired.
+3
source to share
2 answers
It is set up correctly, but the focus must be on the ion map before it starts listening. Click on the card and then press a key and it should work. If you want focus on the entire page, check this question: Angular 2 | listening for a keypress event on the whole page
+1
source to share