Open In App

Angular PrimeNG Sidebar Events

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 learn about Angular PrimeNG Sidebar Events.

The Sidebar Component is used to make an element that overlays at the edges of the screen.

Angular PrimeNG Sidebar Events:

  • onShow: It is a callback that is fired when a dialog is shown.
  • onHide: It is a callback that is fired when a dialog is hidden.

 

Syntax:

<p-sidebar 
    [(visible)]="...">
    ...
</p-sidebar>

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

 

Steps to run the application: Run the below command to see the output

ng serve --save

Example 1: In this example, we will learn about Angular PrimeNG Sidebar Events onShow.

  • app.component.html:

HTML




<h1 style="color: green;">GeeksforGeeks</h1>
<h2>Angular PrimeNG Sidebar Events</h2>
  
<p-sidebar [(visible)]="gfg" 
    [baseZIndex]="10000" (onShow)="onShow()">
    <h1 style="font-weight: normal;">GeeksforGeeks</h1>
    <p>Angular PrimeNG Sidebar Events</p>
  
    <p-button
        type="button"
        (click)="gfg = false"
        label="OK"
        styleClass="p-button-info">
    </p-button>
    <p-button
        type="button"
        (click)="gfg = false"
        label="Cancel"
        styleClass="p-button-danger">
    </p-button>
</p-sidebar>
<p-button 
        (click)="gfg = true
        label="Click Here!">
</p-button>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
  
export class AppComponent {
    gfg:boolean=false;
    onShow(){
        alert("Hi Geek!! On Show is called")
    }
}


  • 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 { ButtonModule } from "primeng/button";
import { SidebarModule } from "primeng/sidebar";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        SidebarModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

Example 2: In this example, we will learn about Angular PrimeNG Sidebar Events onHide.

  • app.component.html:

HTML




<h1 style="color: green;">GeeksforGeeks</h1>
<h2>Angular PrimeNG Sidebar Events</h2>
  
<p-sidebar [(visible)]="gfg" 
    [baseZIndex]="10000" (onHide)="onHide()">
    <h1 style="font-weight: normal;">GeeksforGeeks</h1>
    <p>Angular PrimeNG Sidebar Events</p>
  
    <p-button
        type="button"
        (click)="gfg = false"
        label="OK"
        styleClass="p-button-info">
    </p-button>
    <p-button
        type="button"
        (click)="gfg = false"
        label="Cancel"
        styleClass="p-button-danger">
    </p-button>
</p-sidebar>
<p-button 
        (click)="gfg = true
        label="Click Here!">
</p-button>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
  
export class AppComponent {
    gfg:boolean=false;
    onHide(){
        alert("Hi Geek!! On Hide is called")
    }
}


  • 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 { ButtonModule } from "primeng/button";
import { SidebarModule } from "primeng/sidebar";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        SidebarModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

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



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