Angular PrimeNG is an open-source library that consists of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will be seeing Angular PrimeNG Menu Events.
The Menu component is used for navigating around the web application and supports both static and dynamic positioning. The two events of the Menu component named onShow and onHide are described below.
Angular PrimeNG Menu Events:
- onShow: This event accepts a callback to invoke when the overlay menu is shown.
- onHide: This event accepts a callback to invoke when the overlay menu is hidden.
Syntax:
<p-menu (menu-event)="callback()" [model]="..." [popup]="true"> </p-menu>
Creating Angular Application and Installing the Module:
Step 1: Create an Angular application using the following command.
ng new appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Step 3: Finally, Install PrimeNG in your given directory.
npm install primeng --save npm install primeicons --save
Project Structure: The project Structure will look like this after following the above steps:
Example 1: This example illustrates the use of the onShow event of a menu in popup mode and shows a toast message when the event fires.
app.component.html:
< h1 style = "color:green" >
GeeksforGeeks
</ h1 >
< h3 >
Angular PrimeNG Menu Events
</ h3 >
< p-button icon = "pi pi-window-maximize" label = "Open Menu" (click)="myMenu.toggle($event)">
</ p-button >
< p-menu
#myMenu
[model]="navigation"
[popup]="true"
(onShow)="menuOnShow()">
</ p-menu >
< p-toast ></ p-toast >
|
app.component.ts:
import { Component } from '@angular/core' ;
import { MenuItem, MessageService } from 'primeng/api'
@Component({ selector: 'app-root' ,
templateUrl: './app.component.html' ,
styleUrls: [ './app.component.css' ],
providers: [MessageService]
}) export class AppComponent { constructor(private msg: MessageService){}
navigation: MenuItem[] = [];
ngOnInit()
{
this .navigation = [
{
label: "Home" ,
icon: "pi pi-home"
},
{
label: "Blog" ,
icon: "pi pi-book"
},
{
label: "Tools" ,
icon: "pi pi-cog"
},
{
label: "Cart" ,
icon: "pi pi-shopping-cart"
},
{
label: "Search" ,
icon: "pi pi-search"
}
]
}
menuOnShow()
{
this .msg.add({
severity: "success" ,
summary: "Menu Shown" ,
detail: "onShow Event Triggered"
})
}
} |
app.module.ts:
import { NgModule } from '@angular/core' ;
import { BrowserModule } from '@angular/platform-browser' ;
import { HttpClientModule } from '@angular/common/http' ;
import { BrowserAnimationsModule } from '@angular/platform-browser/animations' ;
import { AppComponent } from './app.component' ;
import { FormsModule } from '@angular/forms' ;
import { ToastModule } from "primeng/toast" ;
import { ButtonModule } from "primeng/button" ;
import { MenuModule } from "primeng/menu" ;
@NgModule({ imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
ToastModule,
FormsModule,
MenuModule,
ButtonModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
}) export class AppModule { } |
Output:
Example 2: In the below example, we will use the onHide event of the popup menu to show a toast message when the menu is hidden.
app.component.html:
< h1 style = "color:green" >
GeeksforGeeks
</ h1 >
< h3 >
Angular PrimeNG Menu Events
</ h3 >
< h4 >OnHide Event - PrimeNG Menu</ h4 >
< p-button icon = "pi pi-window-maximize" label = "Open Menu" (click)="myMenu.toggle($event)">
</ p-button >
< p-menu
#myMenu
(onHide)="menuOnHide()"
[model]="navigation"
[popup]="true">
</ p-menu >
< p-toast ></ p-toast >
|
app.component.ts:
import { Component } from '@angular/core' ;
import { MenuItem, MessageService } from 'primeng/api'
@Component({ selector: 'app-root' ,
templateUrl: './app.component.html' ,
styleUrls: [ './app.component.css' ],
providers: [MessageService]
}) export class AppComponent { constructor(private msg: MessageService){}
navigation: MenuItem[] = [];
ngOnInit()
{
this .navigation = [
{
label: "Home" ,
icon: "pi pi-home"
},
{
label: "Blog" ,
icon: "pi pi-book"
},
{
label: "Tools" ,
icon: "pi pi-cog"
},
{
label: "Cart" ,
icon: "pi pi-shopping-cart"
},
{
label: "Search" ,
icon: "pi pi-search"
}
]
}
menuOnHide()
{
this .msg.add({
severity: "error" ,
summary: "Menu Hidden" ,
detail: "onHide Event Fired"
})
}
} |
app.module.ts:
import { Component } from '@angular/core' ;
import { MenuItem, MessageService } from 'primeng/api'
@Component({ selector: 'app-root' ,
templateUrl: './app.component.html' ,
styleUrls: [ './app.component.css' ],
providers: [MessageService]
}) export class AppComponent { constructor(private msg: MessageService) { }
navigation: MenuItem[] = [];
ngOnInit() {
this .navigation = [
{
label: "Home" ,
icon: "pi pi-home"
},
{
label: "Blog" ,
icon: "pi pi-book"
},
{
label: "Tools" ,
icon: "pi pi-cog"
},
{
label: "Cart" ,
icon: "pi pi-shopping-cart"
},
{
label: "Search" ,
icon: "pi pi-search"
}
]
}
menuOnHide() {
this .msg.add({
severity: "error" ,
summary: "Menu Hidden" ,
detail: "onHide Event Fired"
})
}
} |
Output:
Reference: https://www.primefaces.org/primeng/menu