Open In App

Angular PrimeNG MegaMenu Horizontal

Last Updated : 06 Jan, 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. This article will show us how to use the MegaMenu Horizontal in Angular PrimeNG. We will also learn about the properties, and style along with their syntaxes that will be used in the code.

MegaMenu component is a navigation component that is used to make a component with multiple numbers of the menu.

Angular PrimeNG MegaMenu Horizontal properties:

  • model: It is an array of menuitems. It accepts the array data type as input & the default value is null.
  • orientation: It is used to define the orientation. It is of string data type & the default value is horizontal.

Syntax:

<p-megaMenu 
    [model]="..." 
    orientation="horizontal">
</p-megaMenu>

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:

 

Run the below command to see the output.

ng serve --open

Example 1: Below is the example that illustrates the use of Angular PrimeNG MegaMenu Horizontal.

app.component.html




<h2 style="color: green">GeeksforGeeks</h2>
<h5>Angular PrimeNG MegaMenu Horizontal</h5>
  
<p-megaMenu 
   [model]="gfg" 
   orientation="horizontal">
</p-megaMenu>


app.component.ts




import { Component } from '@angular/core';
import { MegaMenuItem } from 'primeng/api';
  
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
})
  
export class AppComponent {
 gfg: MegaMenuItem[];
  
 ngOnInit() {
   this.gfg = [
     {
       label: 'DSA - Self Paced',
       items: [
         [
           {
             label: 'Batch 1',
             items: [{ label: 'Batch 1.1' }, 
             { label: 'Batch 1.2' }],
           },
           {
             label: 'Batch 2',
             items: [{ label: 'Batch 2.1' }, 
             { label: 'Batch 2.2' }],
           },
         ],
         [
           {
             label: 'Batch 3',
             items: [{ label: 'Batch 3.1' }, 
             { label: 'Batch 3.2' }],
           },
           {
             label: 'Batch 4',
             items: [{ label: 'Batch 4.1' }, 
             { label: 'Batch 4.2' }],
           },
         ],
       ],
     },
  
     {
       label: 'Complete Interview Preparation',
       items: [
         [
           {
             label: 'Batch 1',
             items: [{ label: 'User 1.1' }, 
             { label: 'User 1.2' }],
           },
           {
             label: 'Batch 2',
             items: [{ label: 'Batch 2.1' }, 
             { label: 'Batch 2.2' }],
           },
         ],
         [
           {
             label: 'Batch 3',
             items: [{ label: 'Batch 3.1' }, 
             { label: 'Batch 3.2' }],
           },
           {
             label: 'Batch 4',
             items: [{ label: 'Batch 4.1' }, 
             { label: 'Batch 4.2' }],
           },
         ],
         [
           {
             label: 'Batch 5',
             items: [{ label: 'Batch 5.1' }, 
             { label: 'Batch 5.2' }],
           },
           {
             label: 'Batch 6',
             items: [{ label: 'Batch 6.1' }, 
             { label: 'Batch 6.2' }],
           },
         ],
       ],
     },
   ];
 }
}


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


Output:

 

Example 2: Below is another example that illustrates the use of Angular PrimeNG MegaMenu Horizontal.

app.component.html




<h2 style="color: green">GeeksforGeeks</h2>
<h5>Angular PrimeNG MegaMenu Horizontal</h5>
  
<p-megaMenu 
   [model]="geek" 
   orientation="horizontal">
</p-megaMenu>


app.component.ts




import { Component } from '@angular/core';
import { MegaMenuItem } from 'primeng/api';
  
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
})
export class AppComponent {
 geek: MegaMenuItem[];
  
 ngOnInit() {
   this.geek = [
     {
       label: 'Algorithms Tutorials',
       items: [
         [
           {
             label: 'Analysis of Algorithms',
             items: [
               { label: 'Asymptotic Analysis' },
               { label: 'Worst, Average and Best Cases' },
             ],
           },
         ],
         [
           {
             label: 'Sorting ALgorithms',
             items: [{ label: 'Merge Sort' }, 
             { label: 'Quick Sort' }],
           }
         ],
       ],
     },
  
     {
       label: 'CS Subject Tutorials',
       items: [
         [
           {
             label: 'System Design',
             items: [{ label: 'Batch 1.1' }, 
             { label: 'Batch 1.2' }],
           },
         ],
         [
           {
             label: 'C++ STL',
             items: [{ label: 'Batch 1.1' }, 
             { label: 'Batch 1.2' }],
           },
         ],
       ],
     },
   ];
 }
}


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


Output:

 

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



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

Similar Reads