Open In App

Angular PrimeNG Menu Component

Last Updated : 13 Feb, 2023
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. In this article, we will know how to use the Menu component in Angular PrimeNG. We will also learn about the properties, events, methods & styling along with their syntaxes that will be used in the code. 

Menu component: It is used to make a component that contains some information & supports either static or dynamic positioning.

Properties:

  • model: It is an array of menuitems. It accepts the array data type as input & the default value is null.
  • popup: It is used to define if the menu would be displayed as a popup. It is of boolean data type & the default value is false.
  • style: It is used to set the inline style of the component. It is of string data type & the default value is null.
  • styleClass: It is used to set the style class of the component. It is of string data type & the default value is null.
  • appendTo: It is the target element to attach the overlay. It accepts any data type & the default value is null.
  • baseZIndex: It is used to set base zIndex value to use in layering.  It is of number data type & the default value is 0.
  • autoZIndex: It is used to specify whether to automatically manage the layering. It is of boolean data type & the default value is true.
  • showTransitionOptions: It is used to show transition options of the show animation. It is of string data type & the default value is .12s cubic-bezier(0, 0, 0.2, 1).
  • hideTransitionOptions: It is used to hide transition options of the hide animation. It is of string data type & the default value is .1s linear.

Events:

  • onShow: It is a callback that is fired when the overlay menu is shown.
  • onHide: It is a callback that is fired when the overlay menu is hidden.

 

Methods:

  • toggle: It is used to toggles the visibility of the popup menu.
  • show: It is used to displays the popup menu.
  • hide: It is used to hides the popup menu.

Styling:

  • p-menu: it is a container element.
  • p-menu-list: it is a list element.
  • p-menuitem: it is a menuitem element.
  • p-menuitem-text: it is a label of a menuitem.
  • p-menuitem-icon: it is an icon of a menuitem.

Creating Angular application & module installation:

  • 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: Install PrimeNG in your given directory.
npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

Example 1: This is the basic example that shows how to use the Menu component.

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG Menu Component</h5>
<p-menu [model]="gfg"></p-menu>


 

app.module.ts




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
  
import { AppComponent } from './app.component';
import { MenuModule } from 'primeng/menu';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
  imports: [BrowserModule, 
              BrowserAnimationsModule, 
            MenuModule, ButtonModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}


app.component.ts




import { Component } from '@angular/core';
import { MenuItem } from 'primeng/api';
  
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html'
})
export class AppComponent {
  gfg: MenuItem[];
  
  ngOnInit() {
    this.gfg = [
      {
        label: 'HTML',
        items: [
          {
            label: 'HTML 1'
          },
          {
            label: 'HTML 2'
          }
        ]
      },
      {
        label: 'Angular',
  
        items: [
          {
            label: 'Angular 1'
          },
          {
            label: 'Angular 2'
          }
        ]
      }
    ];
  }
}


Output:

Example 2: In this example, we are making a menu list using a button.

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG Menu Component</h5>
<button type="button" pButton pRipple label="Click Here"
        (click)="menu.toggle($event)"></button>
<p-menu #menu [popup]="true" [model]="gfg"></p-menu>


app.module.ts




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
  
import { AppComponent } from './app.component';
import { MenuModule } from 'primeng/menu';
import { ButtonModule } from 'primeng/button';
import { RippleModule } from 'primeng/ripple';
  
@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MenuModule,
    RippleModule,
    ButtonModule
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}


app.component.ts




import { Component } from '@angular/core';
import { MenuItem, MessageService, PrimeNGConfig } from 'primeng/api';
  
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  providers: [MessageService]
})
export class AppComponent {
  gfg: MenuItem[];
  
  constructor(
    private messageService: MessageService,
    private primengConfig: PrimeNGConfig
  ) {}
  
  ngOnInit() {
    this.primengConfig.ripple = true;
  
    this.gfg = [
      {
        label: 'HTML',
        items: [
          {
            label: 'HTML 1'
          },
          {
            label: 'HTML 2'
          }
        ]
      },
      {
        label: 'Angular',
  
        items: [
          {
            label: 'Angular 1'
          },
          {
            label: 'Angular 2'
          }
        ]
      }
    ];
  }
}


Output:

Reference: https://primeng.org/menu



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

Similar Reads