Open In App

Angular PrimeNG ContextMenu Component

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we are going to learn the Angular PrimeNG ContextMenu Component.

The ContextMenu component displays a menu when right-clicking over any component. Many components provide a special integration with the ContextMenu component. The ContextMenu component appears similar to the menu that appears when we right-click over any application or desktop screen on a PC. 



Angular PrimeNG ContextMenu Components:

Syntax:



Import the ContextMenu module.

import {ContextMenuModule} from 'primeng/contextmenu';

Use it with the target component as follows:

<p-contextMenu 
    [target]="..." 
    [model]="..." >
</p-contextMenu>

<img #gfglogo src="..." alt="...">

Creating Angular application & Module Installation:

Step 1: Create an Angular application using the following command:

ng new geeks_angular

Step 2:  After creating your project folder i.e. geeks_angular, move to it using the following command.

cd geeks_angular

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: The project structure will look like the following:

 

Example 1: In the following example, we have a basic ContextMenu component attached to the Image component.

app.component.html:




<h1 style="color: green; text-align: center;">
    GeeksforGeeks
</h1>
<h3>Angular PrimeNG ContextMenu Component</h3>
  
<img #img src=
    alt="Logo" aria-haspopup="true"
/>
  
<p-contextMenu [target]="img" [model]="items"></p-contextMenu>

app.component.ts:




import { Component } from "@angular/core";
import { MenuItem } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {
    items: MenuItem[];
  
    ngOnInit() {
        this.items = [
            {
                label: "Visit Website",
                icon: "pi pi-fw pi-globe"
            },
            {
                label: "Like",
                icon: "pi pi-fw pi-thumbs-up-fill"
            },
            {
                label: "More",
                icon: "pi pi-fw pi-folder",
                items: [
                    {
                        label: "New",
                        icon: "pi pi-fw pi-plus"
                    },
                    {
                        label: "Share",
                        icon: "pi pi-fw pi-share-alt"
                    },
                    {
                        label: "Search",
                        icon: "pi pi-fw pi-search"
                    }
                ]
            },
  
            {
                separator: true
            },
            {
                label: "Quit",
                icon: "pi pi-fw pi-power-off"
            }
        ];
    }
}

app.module.ts:




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { RouterModule } from "@angular/router";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ContextMenuModule } from "primeng/contextmenu";
  
@NgModule({
    imports: [
        BrowserModule, 
        BrowserAnimationsModule, 
        ContextMenuModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}

Output:

 

Example 2: In the following example, we have made the context menu global, that is the ContextMenu component will display by right-clicking anywhere in the document.

app.component.html:




<h1 style="color: green; text-align: center;">
    GeeksforGeeks
</h1>
<h3>Angular PrimeNG ContextMenu Component</h3>
<img #img src=
  alt="Logo" aria-haspopup="true"/>
  
<p-contextMenu [global]="true" [target]="img" [model]="items"></p-contextMenu>

app.component.ts:




import { Component } from "@angular/core";
import { MenuItem } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {
    items: MenuItem[];
  
    ngOnInit() {
        this.items = [
            {
                label: "Visit Website",
                icon: "pi pi-fw pi-globe"
            },
            {
                label: "Like",
                icon: "pi pi-fw pi-thumbs-up-fill"
            },
            {
                label: "More",
                icon: "pi pi-fw pi-folder",
                items: [
                    {
                        label: "New",
                        icon: "pi pi-fw pi-plus"
                    },
                    {
                        label: "Share",
                        icon: "pi pi-fw pi-share-alt"
                    },
                    {
                        label: "Search",
                        icon: "pi pi-fw pi-search"
                    }
                ]
            },
  
            {
                separator: true
            },
            {
                label: "Quit",
                icon: "pi pi-fw pi-power-off"
            }
        ];
    }
}

app.module.ts:




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { RouterModule } from "@angular/router";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ContextMenuModule } from "primeng/contextmenu";
  
@NgModule({
    imports: [
        BrowserModule, 
        BrowserAnimationsModule, 
        ContextMenuModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}

Output:

 

Reference: https://primefaces.org/primeng/contextmenu


Article Tags :