Open In App

Angular PrimeNG ConfirmDialog Templates

Last Updated : 30 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Angular PrimeNG is a framework used with angular to create components with great styling this framework is very easy to use and is used to make responsive websites. In this article, we will know how to use ConfirmDialog Templates in Angular PrimeNG. 

The ConfirmDialog Component is used to make a dialog box containing confirm button to confirm the element. This article will show the ConfirmDialog Templating in Angular PrimeNG. The templates are used to put some content on some pre-structured containers. 

ConfirmDialog templates are discussed below:

  • header: This is used to place the header of the ConfirmDialog component.
  • footer: This is used to place the footer of the ConfirmDialog component.

 

Creating Angular Application And Installing Module:

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: To run the above file, run the below command:

ng serve --save

Example 1: Below is the example code that illustrates the use of Angular PrimeNG ConfirmDialog Templates using the header Template.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG ConfirmDialog Templates.</h5>
  
<p-confirmDialog #cd [style]="{width: '60vw'}">
    <ng-template pTemplate="header">
        <h3>GeeksforGeeks</h3>
    </ng-template>
</p-confirmDialog>
  
<p-button (click)="gfg()"
    icon="pi pi-code"
    label="Click me Geek!">
</p-button>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
import {ConfirmationService} from 'primeng/api';
import {Message} from 'primeng/api';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [ConfirmationService]
})
  
export class AppComponent {
    msgs: Message[] = [];
  
    constructor(private confirmationService: ConfirmationService,
        private primengConfig: PrimeNGConfig
    ){}
  
    gfg() {
        this.confirmationService.confirm({
            message: 'Hey Geek, Are you sure?',
        });
    }
}


  • app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent }   from './app.component';
import { ButtonModule } from 'primeng/button';
import { MessagesModule } from 'primeng/messages';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
  
@NgModule({
    imports: [
        BrowserAnimationsModule,
        ConfirmDialogModule,
        ButtonModule,
        MessagesModule,
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
  
export class AppModule { }


Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG ConfirmDialog Templates using both header and footer Templates.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG ConfirmDialog Templates.</h5>
  
<p-confirmDialog #cd [style]="{width: '60vw'}">
    <ng-template pTemplate="header">
        <h3>GeeksforGeeks</h3>
    </ng-template>
  
    <ng-template pTemplate="footer">
        <button type="button" pButton
            icon="pi pi-times" label="reject me!"
            (click)="cd.reject()">
        </button>
      
        <button type="button" pButton
            icon="pi pi-check" label="confirm me!"
            (click)="cd.accept()">
        </button>
    </ng-template>
</p-confirmDialog>
  
<p-button (click)="gfg()"
    icon="pi pi-code"
    label="Click me Geek!">
</p-button>
  
<p-messages [value]="msgs"></p-messages>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
import {ConfirmationService} from 'primeng/api';
import {Message} from 'primeng/api';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [ConfirmationService]
})
  
export class AppComponent {
    msgs: Message[] = [];
  
    constructor(private confirmationService: ConfirmationService,
        private primengConfig: PrimeNGConfig
    ){}
  
    gfg() {
        this.confirmationService.confirm({
            message: 'Hey Geek, Are you sure?',
            accept: () => {
                this.msgs = [{
                    severity:'success',
                    summary:'Accepted!',
                    detail:'Geeks has accepted.'
                }];
            },
            reject: () => {
                this.msgs = [{
                    severity:'error',
                    summary:'Refused',
                    detail:'Geeks has Refused.'
                }];
            }
        });
    }
}


  • app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent }   from './app.component';
import { ButtonModule } from 'primeng/button';
import { MessagesModule } from 'primeng/messages';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
  
@NgModule({
    imports: [
        BrowserAnimationsModule,
        ConfirmDialogModule,
        ButtonModule,
        MessagesModule,
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
  
export class AppModule { }


Output:

 

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



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

Similar Reads