Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Angular 4 context menu

How do you create a context menu in angular 4 ? Unfortunately the html context menu doesn't work.

So I want to create a component and display it on right click at the cursor coordinates but how do you implement this?

e.g.

<ul>     <li (click)="showContextMenuComponent">example</li> </ul 
like image 387
User9132 Avatar asked Aug 15 '17 22:08

User9132


People also ask

How to use context menu 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 do you create a context menu in HTML?

The contextmenu global attribute is the id of a <menu> to use as the contextual menu for this element. A context menu is a menu that appears upon user interaction, such as a right-click. HTML now allows us to customize this menu. Here are some implementation examples, including nested menus.

When you right click on any icon a pop up menu appears which is called?

A context menu (also know as a contextual menu, shortcut menu or pop-up menu) is the menu that appears when you right-click and offers a set of choices that are available for, or in context of, whatever it was you clicked.


2 Answers

I found all your solutions quite complicated and hard to customize, and since i just started i wanted to solve this with components and eventbinding only. So My ContextMenu is a component with Input values x and y and is shown on right click on top of its ParentComponent :)

Stackblitz Example

So here it is:

Parent.component.ts

 export class parentComponent {       contextmenu = false;       contextmenuX = 0;       contextmenuY = 0;        //activates the menu with the coordinates       onrightClick(event){           this.contextmenuX=event.clientX           this.contextmenuY=event.clientY           this.contextmenu=true;       }       //disables the menu       disableContextMenu(){          this.contextmenu= false;       } 

parent.component.html

<!-- your whole html is wrapped in a div so anywhere you click you disable contextmenu, also the div is responsible for suppressing the default browser contextmenu --> <div (click)="disableContextMenu()" oncontextmenu="return false;">     <!-- this is the usage -->     <ul>         <li (contextmenu)="onrightClick($event)">right click me!</li>     </ul>      <!--you have to write this only once in your component-->     <div *ngIf="contextmenu">         <app-contextmenu [x]="contextmenuX" [y]="contextmenuY"></app-contextmenu>     </div> </div> 

This is the context menu itself:

contextmenu.component.ts

import { Component, Input } from '@angular/core';  @Component({   selector: 'app-contextmenu', }) export class ContextmenuComponent{    constructor() { }     @Input() x=0;   @Input() y=0;  } 

contextmenu.component.html

<div class="contextmenu" [ngStyle]="{'left.px': x, 'top.px': y}">   this is your contextmenu content </div> 

contextmenu.component.css

.contextmenu{     position: absolute; } 

You can now apply your own animations, css styling, etc. as usual with a component. Hope this could help :) have fun !

like image 56
User9132 Avatar answered Sep 20 '22 16:09

User9132


You can try ngx-contextmenu library. Check the demo here

If you are on angular version 4 consider using [email protected]

like image 31
angularrocks.com Avatar answered Sep 19 '22 16:09

angularrocks.com