Open In App

Angular PrimeNG Dialog Overlays Inside

Last Updated : 25 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 Angular PrimeNG Dialog Overlays Inside.

The Dialog Component is used to make a component containing some content to display in an overlay window.

Angular PrimeNG Dialog Overlays Inside: As dialog display content in an overlay window, now suppose the content in the dialog is a dropdown. Then due to the overflow property of the dialog, the content won’t go outside the boundary of the dialog. This makes the dropdown in the dialog look very bad.
 In order to solve this, you can either append the overlay to the body or allow overflow in the dialog.

 

Syntax:

<p-dialog>
    <p-dropdown 
        appendTo="body">
    </p-dropdown>
</p-dialog>

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 Dialog Overlays Inside and see how appendTo works with dropdown.

  • app.component.html:

HTML




<div style="width: 50%;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h2>
        Angular PrimeNG Dialog Overlays Inside
    </h2>
    <p-button (click)="gfg()" 
        label="Click Here">
      </p-button>
    <p-dialog header="GeeksforGeeks" [(visible)]="geeks">
        <p>Angular PrimeNG Dialog Overlays Inside</p>
        <p-dropdown
            [options]="cities"
            appendTo="body"
            placeholder="Select a City"
            optionLabel="name"
            [showClear]="true">
        </p-dropdown>
    </p-dialog>
</div>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { PrimeNGConfig } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
})
  
export class AppComponent {
    cities: City[]=[];
        constructor() {
            this.cities = [
                {name: 'New York', code: 'NY'},
                {name: 'Rome', code: 'RM'},
                {name: 'London', code: 'LDN'},
                {name: 'Istanbul', code: 'IST'},
                {name: 'Paris', code: 'PRS'}
            ];
        }
    geeks: boolean=false;
    gfg() {
        this.geeks = true;
    }
}
  
export interface City {
    name: string,
    code: string
}


  • app.module.ts:

Javascript




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


Output:

 

Example 2: In this example, we will see, what if we don’t use appendTo with dropdown. how it will appear.

  • app.component.html:

HTML




<div style="width: 50%;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h2>
        Angular PrimeNG Dialog Overlays Inside
    </h2>
    <p-button (click)="gfg()" label="Click Here"></p-button>
    <p-dialog header="GeeksforGeeks" [(visible)]="geeks">
        <p>Angular PrimeNG Dialog Overlays Inside</p>
        <p-dropdown
            [options]="cities"
            placeholder="Select a City"
            optionLabel="name"
            [showClear]="true">
        </p-dropdown>
    </p-dialog>
</div>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { PrimeNGConfig } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
})
  
export class AppComponent {
    cities: City[]=[];
    constructor() {
        this.cities = [
            {name: 'New York', code: 'NY'},
            {name: 'Rome', code: 'RM'},
            {name: 'London', code: 'LDN'},
            {name: 'Istanbul', code: 'IST'},
            {name: 'Paris', code: 'PRS'}
        ];
    }
    geeks: boolean=false;
    gfg() {
        this.geeks = true;
    }
}
  
export interface City {
    name: string,
    code: string
}


  • app.module.ts:

Javascript




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


Output:

 

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



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

Similar Reads