Open In App

Angular PrimeNG MenuModel API MenuItem

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 see how to use Angular PrimeNG MenuModel API MenuItem.

MenuModel API: PrimeNG menus components share a common API to specify the menuitems and submenus.



The MenuItem class defines various options, like the label, icon, and children of an item in a menu. 

Syntax:



export class MenuDemo {

    private items: MenuItem[];

    ngOnInit() {
        this.gfg= [{
            label: '...',
            items: [
                
            ]
        }]
      }
}

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: In this example, we will create a MenuItem such that label contains an HTML value rather than the string.




<div style="width:80%">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h2>
        Angular PrimeNG MenuModel API MenuItem
    </h2>
    <p-menu [model]="gfg"></p-menu>
</div>




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




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';
  
@NgModule({
    imports: [BrowserModule,
              BrowserAnimationsModule,
              MenuModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

Output:

 

Example 2: In this example, we will create MenuItem with icons.




<div style="width:80%">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h2>
        Angular PrimeNG MenuModel API MenuItem
    </h2>
    <p-menu [model]="gfg"></p-menu>
</div>




import { Component } from '@angular/core';
import { MenuItem } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    gfg: MenuItem[] = [];
  
    ngOnInit() {
        this.gfg = [
            {
                label: "Home",
                icon: "pi pi-home"
            },
            {
                label: "About Us",
                icon: "pi pi-info"
            },
            {
                label: "Contact",
                icon: "pi pi-phone"
            }
        ];
    }
}




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';
  
@NgModule({
    imports: [BrowserModule,
              BrowserAnimationsModule,
              MenuModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

Output:

 

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


Article Tags :