Open In App

Angular PrimeNG Slide Menu Methods

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. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we will see the Angular PrimeNG Slide Menu Methods.

The Slide Menu component allows the creation of the menu in the form of a tier. It displays submenus in nested overlays. The Slide Menu Methods are used to control the functionality of the component with the help of component-defined methods.



Angular PrimeNG Slide Menu Methods:

 



Syntax:

//app.component.html
<p-slideMenu #gfgmenu 
            [model]="..." 
            [popup]="...">
</p-slideMenu>
<button #btn
        type="button" pButton
        label="...."
        (click)="toggleClick($event)">
</button>

// app.component.ts
toggleClick(event) {
  this.slideMenu.toggle(event);
}

Creating Angular application & Module Installation:

Step 1: Create an Angular application using the following command.

ng new geeks_angular

Step 2: After creating your project folder i.e. geeks_angular, move to it using the following command.

cd geeks_angular

Step 3: Install PrimeNG and CDK in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: The project structure will look like the following:

 

ng serve start

Example 1: In the following example, we have a SlideMenu with a toggle button to toggle it.




<h1 style="color: green; 
           text-align: center;">
      GeeksforGeeks
</h1>
<h3>
  Angular PrimeNG SlideMenu Methods
</h3>
  
<p-slideMenu #gfgmenu [model]="items" 
                      [popup]="true">
</p-slideMenu>
<button #btn type="button"
             pButton
             icon="pi pi-arrow-down"
             label="Toggle"
             (click)="toggleClick($event)">
</button>




import { Component, OnInit, ViewChild,
         ViewEncapsulation} 
    from "@angular/core";
import { MenuItem } from "primeng/api";
import { SlideMenu } from "primeng/slidemenu";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.css"]
})
  
export class AppComponent {
    items: MenuItem[];
    @ViewChild("gfgmenu") slideMenu!: SlideMenu;
    ngOnInit() {
        this.items = [
            {
                label: "Data Structures",
                items: [
                    {
                        label: "Linked List",
                        items: [
                            {
                                label: "Singly Linked List"
                            },
                            {
                                label: "Doubly Linked List"
                            }
                        ]
                    },
                    {
                        label: "Trie"
                    },
                    {
                        label: "Array"
                    }
                ]
            },
            {
                label: "Algorithms",
                items: [
                    {
                        label: "Sorting"
                    },
                    {
                        label: "Searching"
                    }
                ]
            },
            {
                label: "Development",
  
                items: [
                    {
                        label: "Android"
                    },
                    {
                        label: "Web development"
                    }
                ]
            }
        ];
    }
    toggleClick(event) {
        this.slideMenu.toggle(event);
    }
}




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

Output:

 

Example 2: In the following example, we have used the toggle and show methods.




<h1 style="color: green; 
           text-align: center;">
  GeeksforGeeks
</h1>
<h3>
  Angular PrimeNG SlideMenu Methods
</h3>
  
<p-slideMenu #gfgmenu [model]="items" 
                      [popup]="true">
</p-slideMenu>
<button #btn type="button"
             pButton
             icon="pi pi-globe"
             label="Show"
             style="margin-right: 20px;"
             (click)="showClick($event)">
</button>
<button #btn type="button"
             pButton
             icon="pi pi-arrow-down"
             label="Toggle"
             (click)="toggleClick($event)">
</button>




import {
    Component,
    OnInit,
    ViewChild,
    ViewEncapsulation
} from "@angular/core";
  
import { MenuItem } from "primeng/api";
import { SlideMenu } from "primeng/slidemenu";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.css"]
})
  
export class AppComponent {
    items: MenuItem[];
    @ViewChild("gfgmenu") slideMenu!: SlideMenu;
    ngOnInit() {
        this.items = [
            {
                label: "Data Structures",
                items: [
                    {
                        label: "Linked List",
                        items: [
                            {
                                label: "Singly Linked List"
                            },
                            {
                                label: "Doubly Linked List"
                            }
                        ]
                    },
                    {
                        label: "Trie"
                    },
                    {
                        label: "Array"
                    }
                ]
            },
            {
                label: "Algorithms",
                items: [
                    {
                        label: "Sorting"
                    },
                    {
                        label: "Searching"
                    }
                ]
            },
            {
                label: "Development",
  
                items: [
                    {
                        label: "Android"
                    },
                    {
                        label: "Web development"
                    }
                ]
            }
        ];
    }
    showClick(event) {
        this.slideMenu.show(event);
    }
    toggleClick(event) {
        this.slideMenu.toggle(event);
    }
}




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

Output:

 

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


Article Tags :