Open In App

Angular PrimeNG OverlayPanel Show and Hide

Last Updated : 31 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. In this article, we will know how to use Dynamic OverlayPanel Show and Hide in Angular PrimeNG.

OverlayPanel is a container component positioned as connected to its target. It appears normally when a button is clicked as a pop-up with some action to take as selecting an item and then closing it. It is said to be dynamic because it does not appear on the first mount but appears on interaction with the website.

The show method requires two parameters, the first of which is the mandatory event. If you would like to align the overlay to another element, provide it as the second parameter. By default, the event target serves as the target component. The overlay panel is also hidden when you call hide(), and the toggle method changes the panel’s visibility. In the example below, clicking the button actually brings up the overlayPanel, not the button itself, which is aligned with the actualTarget div.

 

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: Below is the example code that illustrates the use of Angular PrimeNG Dynamic OverlayPanel Show and Hide.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Dynamic OverlayPanel Show and Hide</h5>
  
<p-overlayPanel 
    #gfg [style]="{ width: '350px' }">
    <h1>GeeksforGeeks</h1>
    <p>It is an Computer Science Portal for all Geeks</p>
</p-overlayPanel>
  
<button
    label="Show OverlayPanel"
    pButton
    type="button"
    (click)="gfg.show($event)">
</button>
   
<button
    label="Hide overlayPanel"
    class="p-button-danger"
    (click)="gfg.hide($event)"
    pButton
    type="button">
</button>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
  
export class AppComponent { }


  • app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent }   from './app.component';
import {ProductService} from './productservice';
import { ButtonModule } from 'primeng/button';
import { OverlayPanelModule } from 'primeng/overlaypanel';
import { MessageService } from 'primeng/api';
  
@NgModule({
    imports: [
        BrowserAnimationsModule,
        OverlayPanelModule,
        ButtonModule,
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ],
    providers: [ProductService, MessageService]
})
  
export class AppModule { }


Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG Dynamic OverlayPanel Show and Hide with tabPanel.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Dynamic OverlayPanel Show and Hide</h5>
  
<p-overlayPanel #gfg [style]="{ width: '350px' }">
    <p-tabView>
        <p-tabPanel header="Geek I">
            <p> Geek 1 </p>
        </p-tabPanel>
        <p-tabPanel header="Geek II">
            <p> Geek 2 </p>
        </p-tabPanel>
    </p-tabView>
</p-overlayPanel>
  
<button
    label="Show OverlayPanel"
    pButton
    type="button"
    (click)="gfg.show($event)">
</button>
   
<button
    label="Hide overlayPanel"
    class="p-button-danger"
    (click)="gfg.hide($event)"
    pButton
    type="button">
</button>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
  
export class AppComponent { }


  • app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent }   from './app.component';
import {ProductService} from './productservice';
import { ButtonModule } from 'primeng/button';
import { OverlayPanelModule } from 'primeng/overlaypanel';
import { MessageService } from 'primeng/api';
import {TabViewModule} from 'primeng/tabview';
  
  
@NgModule({
    imports: [
        BrowserAnimationsModule,
        OverlayPanelModule,
        ButtonModule,
        TabViewModule
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ],
    providers: [ProductService, MessageService]
})
  
export class AppModule { }


Output:

 

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



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

Similar Reads