Open In App

Angular PrimeNG Dynamic OverlayPanel 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.  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:

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.




<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>




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




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.




<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>




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




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


Article Tags :