Open In App

Angular PrimeNG ContextMenu Component

Last Updated : 29 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • MenuModel API: ContextMenu uses the menu model API to display the menu when right-clicked.
  • Target: The ContextMenu is attached to the target component when right-clicking on it.
  • Exclusive Integration: Components like Tree, DataTable, etc. have special integration so they provide a different method to attach a context menu. 
  • Properties: The properties of the component allow us to customize the component and display it accordingly and suit the website design. 
  • Event: The events are used to trigger some specific functions when the user interacts with the component so that the required action can be taken.
  • Methods: The methods are used to call some component functions so that required modifications can be done during the render on some conditions.
  • Styling: The styling classes provided with the component help the developers to modify the default CSS the component arrives with so that the content can be made like the website design and hence making the component more customizable. 

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:

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:

Javascript




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:

Javascript




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:

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:

Javascript




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:

Javascript




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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads