Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 keyup.enter for div tag

I need to trigger one function from selected div enter key is pressed. I wrote the following code but it's not working.

<div class="data_card" *ngIf="!Add.showEdit" (keyup.enter)="myfunction($event)">
<!-- Some content -- >
</div>


myfunction($event){
      $event.preventDefault();
      alert("sadsa");
}

How to trigger this function from div enter key is pressed?

like image 420
user3541485 Avatar asked Jun 26 '17 11:06

user3541485


People also ask

What is Keyup event in Angular 2?

When the user presses the 'Enter' key on the keyboard, Angular 2 calls the keyup event and display the text entered by the user.

How to use double click event in Angular 2?

Angular 2 Double Click (dblclick) event using the EventEmitter, a mother component receives the double click (dblclick) event from the child component when a user clicks twice on an element. 6. Angular 2 Tutorial: Adding HotKeys (Keyboard shortcuts) to a Component

Are keyboard events fired on Div in angular?

Keyboard events on div are not fired. · Issue #5168 · angular/angular · GitHub Sign up for free to subscribe to this conversation on GitHub . Already have an account? Sign in .

What is ng-Keyup in angular?

Definition and Usage. The ng-keyup directive tells AngularJS what to do when the keyboard is used on the specific HTML element. The ng-keyup directive from AngularJS will not override the element's original onkeyup event, both will be executed.


1 Answers

try this code. does this help. html

<div class="data_card" *ngIf="!Add.showEdit" (keydown)="handleKeyboardEvent($event)">

// component

@HostListener('document:keydown', ['$event'])
    handleKeyboardEvent(event: KeyboardEvent): void {
        if (event.keyCode === 13) {
            **// do your code here**
        }
    }
like image 104
Shailesh Ladumor Avatar answered Oct 16 '22 10:10

Shailesh Ladumor