Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 right click events?

I am building an interactive web application with Angular2 and I am looking for a way to capture right clicks on an angular component. I also need to prevent the browser context menu appearing on a right click so I can display my own custom context menu.

I know that in angular 1, you had to create a custom directive to capture a right click event. Is this still the case in Angular 2, or is it built in/is there an easier way of doing it? I've had a look through some previous SO questions but they do not relate to Angular2.

How can I accomplish capturing right clicks and preventing the browser context menu from appearing in Angular2?

like image 782
JavascriptLoser Avatar asked Jan 16 '17 05:01

JavascriptLoser


People also ask

How to add right click event in Angular?

Create a right click context menu inside a dynamic list or table. We need to link each element of a list or table to a right-click context mat-menu. The list elements and their menu are generated only at runtime. To generate dynamically the menu we create an hidden div with the coordinates of the mouse.

How does angular handle click event?

Events are handled in Angular using the following special syntax. Bind the target event name within parentheses on the left of an equal sign, and event handler method or statement on the right. Above, (click) binds the button click event and onShow() statement calls the onShow() method of a component.

How angular double click is implemented?

AngularJS ng-dblclick Directive The ng-dblclick directive tells AngularJS what to do when an HTML element is double-clicked. The ng-dblclick directive from AngularJS will not override the element's original ondblclick event, both are executed.

What does $event mean in angular?

The $event object often contains information the method needs, such as a user's name or an image URL. The target event determines the shape of the $event object. If the target event is a native DOM element event, then $event is a DOM event object, with properties such as target and target.


1 Answers

In Angular 2+, you can capture any event like:

<div (anyEventName)="whateverEventHandler($event)"> </div> 

Note that $event is optional, and the return of the function is the return of the event handler, so, you can return false; to avoid default browser action from the event.

In your case the event name is contextmenu. So, your code can be something like this:

@Component({   selector: 'my-app',   template: `     <div (contextmenu)="onRightClick($event)">       Right clicked       {{nRightClicks}}       time(s).     </div>   `,   // Just to make things obvious in browser   styles: [`     div {       background: green;       color: white;       white-space: pre;       height: 200px;       width: 200px;     }   `] }) export class App {   nRightClicks = 0;    constructor() {   }    onRightClick() {     this.nRightClicks++;     return false;   } } 

Here's a full working example:
http://on.gurustop.net/2E0SD8e

like image 88
Meligy Avatar answered Oct 09 '22 03:10

Meligy