Open In App

Angular PrimeNG MenuModel API Custom Content

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

In MenuModel API, PrimeNG menus components share a common API to specify the menuitems and submenus. In Custom Content, both simple strings and HTML values are supported by the Label of a menuitem. We only need to use escape property to allow HTML.

Syntax:

export class GFG{
    private items: MenuItem[];

    ngOnInit() {
        this.gfg= [{
            label: '<h2>GeeksforGeeks</h2>',
            escape: false,
            //...
        }
    }
}

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 see the basic usage of the Angular PrimeNG MenuModel API Custom Content.

  • app.component.html

HTML




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


  • 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 {
    gfg: MenuItem[] = [];
  
    ngOnInit() {
        this.gfg = [
            {
                label: '<h4>HTML</h4>',
                items: [
                    {
                        label: 'HTML 1'
                    },
                    {
                        label: 'HTML 2'
                    }
                ],
                escape: false
            },
            {
                label: '<h4>Angular</h4>',
  
                items: [
                    {
                        label: 'Angular 1'
                    },
                    {
                        label: 'Angular 2'
                    }
                ],
                escape: false
            }
        ];
    }
}


  • app.module.ts

Javascript




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 a card template in the label of MenuItem in Angular PrimeNG.

  • app.component.html

HTML




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


  • 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 {
    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
            }
        ];
    }
}


  • app.module.ts

Javascript




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 { }


  • styles.css
@import '~bootstrap/dist/css/bootstrap.css';

Output:

 

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



Last Updated : 02 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads