Open In App

Angular PrimeNG Dynamic OverlayPanel Methods

Last Updated : 03 Feb, 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 see Angular PrimeNG Dynamic OverlayPanel Methods.

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 Methods are used to perform some action on the component and display it accordingly.

Angular PrimeNG Dynamic OverlayPanel Methods: OverlayPanel Methods are discussed below:

  • toggle: It is used to Toggle the visibility of the panel.
  • show: It is used to Display the panel.
  • hide: It is used to Hide the panel.

Syntax:

<p-button [label]="'Close Method'"
         [style]="{background:'red'}"
          (click)="op.hide()">
</p-button>
 
<p-overlayPanel #op [showCloseIcon]="true"]>
    <ng-template pTemplate>
       ...
    </ng-template>
</p-overlayPanel>

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 learn about the toggle method.

  • app.component.html

HTML




<div style="width:80%">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h2>
        Angular PrimeNG Dynamic OverlayPanel Methods
    </h2>
    <p-button [label]="'Open Panel'" 
              icon="pi pi-search" 
              (click)="op.toggle($event)">
    </p-button>
  
    <p-overlayPanel #op [showCloseIcon]="true">
        <ng-template pTemplate>
            Welcome to GeeksforGeeks
        </ng-template>
    </p-overlayPanel>
</div>


  • app.component.ts

Javascript




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


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


Output:

 

Example 2: In this example, we will learn about the show and hide methods.

  • app.component.html

HTML




<div style="width:80%">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h2>
        Angular PrimeNG Dynamic OverlayPanel Methods
    </h2>
    <p-button [label]="'Show Method'" 
              icon="pi pi-search" 
              (click)="op.show($event)">
    </p-button>
       
    <p-button [label]="'Close Method'" 
              [style]="{background:'red'}" 
              (click)="op.hide()">
    </p-button>
  
    <p-overlayPanel #op [showCloseIcon]="false">
        <ng-template pTemplate>
            Welcome to GeeksforGeeks
        </ng-template>
    </p-overlayPanel>
</div>


  • app.component.ts

Javascript




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


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


Output:

 

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



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

Similar Reads