Angular2 Templates: How to bind keydown.arrow to whole div instead of only control elements?

Christof Kälin

I have a simple navigator component with some buttons and a date-picker subcomponent. Now the idea is, that I can bind keydown.arrowleft etc. for the whole div (which has css style of 100% height and width). It's template looks like this:

<div id="ck-navigator" (keydown.arrowleft)="decrementDate(); 
    $event.preventDefault()" (keydown.arrowright)="incrementDate(); 
$event.preventDefault()">
    <button (click)="decrementDate()" class="fa fa-2x fa-chevron-left">
    </button>
    <ck-date-picker [date]="date" (dateChange)="changeDate($event)">
    </ck-date-picker>
    <button (click)="incrementDate()" class="fa fa-2x fa-chevron-right"></button>
</div>

I don't show the component code, it does not matter for this question (afaik). This is working, but only if I actively select a button or the date-picker in advance (aka a control element). Is there a possibility to extend the keybinding to the whole div (i.e. if I just click somewhere on the site). Sorry for my lack of understanding the DOM and thanks for any push in the right direction.

Sam

If what you want is to detect key events on the whole page what you need is probably to add @HostListener to your component. It is gona let you manage events in the whole page/window.

Seeing an example.

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

[your @Component stuff ..... ]

@HostListener("window:keydown", ['$event'])

 onKeyDown(event:KeyboardEvent) {

    console.log(`Pressed ${event.key}!`);
  }

Furthermore could help you to know that when you want to refer specific items you can use ElementRef class and ViewChild by importing them from angular/core. Then manage DOM components as:

@ViewChild('DOM_Id') element: ElementRef;

Maybe it helps. Peace

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to format only changed line instead of the whole file in Intelijj IDEA

How can i extend function only instead of whole class in Java?

how to toggle div in jquery with unicode arrow only

How to bind raw html in Angular2

how to combine angular2 with jinja2 templates and flask

How to bind data to label in angular2

Angular2 use `listen` method from `Renderer` class to bind keyboard event like `keydown`

How to install a TFS extension only on some project instead the whole collection

How to control CSS arrow size

How to make a use of arrow function in es6 instead of bind method

Angular: How to use a custom EventPlugin only inside specific component instead of whole ngModule?

How do I capture the keyboard enter/spacebar keydown events for an accordion structure with div and span elements

Angular 7 rxjs 6 bind to certain elements only

How to bind 'this' using templates instead of macros?

How to move the created content of a <div1> into a <div2> without moving the whole <div1> and keep creating elements in <div1>?

Getting only numbers from BeautifulSoup instead of whole div

How to bind arrow keys in C?

How to Sync whole List instead of only visible records

How to bind to `keydown` of another element in a directive

How to bind enabled/disabled status to a control instead of view model?

How to bind a list in Angular2?

Bind devextreme text value to angular model (on keydown)

How to bind a whole module as a function?

How to get a particular value value from JSON and bind the value in HTML elements in angular2

How to use arrow function instead of 'bind' in a helper function

How i can give a indigo color only to a div instead of whole body with help of css?

How to make <div> cover whole page and not only viewport

How to combine keydown.control with a custom click function in Angular?

How display the whole documentation of a class instead of only that of only a single method or class with IntelliJ