Open In App

Angular PrimeNG ConfirmDialog Basic

Last Updated : 10 Aug, 2022
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. This article will show us how to use the ConfirmDialog Basic in Angular PrimeNG

The ConfirmDialog component is used to make a dialog box containing confirm button to confirm the element.

Angular PrimeNG ConfirmDialog Basic properties:

  • style: It is the Inline style of the component. It is of object data type, the default value is null.
  • styleClass: It is the Style class of the component. It is of string data type, the default value is null.
  • acceptButtonStyleClass: It is used to set the Style class of the accept button. It is of string data type, the default value is null.
  • rejectButtonStyleClass: It is used to set the Style class of the reject button. It is of string data type, the default value is null.
  • acceptLabel: It is the Label of the accept button. It is of string data type, the default value is null.
  • rejectLabel: It is the Label of the reject button. It is of string data type, the default value is null.
  • icon: It is the Icon to display next to the message. It is of string data type, the default value is null.
  • header: It is the Header text of the dialog. It is of string data type, the default value is null.
  • accept: It is the Callback to execute when action is confirmed.
  • reject: It is the Callback to execute when an action is rejected

Syntax:

<p-confirmDialog 
    [style]="..." [baseZIndex]="..." 
    rejectButtonStyleClass="p-button-text">
</p-confirmDialog>

<p-button
    (click)="...." 
    label="...">
</p-button>

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.

 

Run the below command to see the output.

ng serve --open

Example 1: Below is the example code that illustrates the use of Angular PrimeNG ConfirmDialog Basic using the confirm button.

app.component.html




<h2 style="color: green">GeeksforGeeks</h2>
<h5>Angular PrimeNG ConfirmDialog Basic</h5>
  
<p-confirmDialog 
    [style]="{ width: '30vw' }">
</p-confirmDialog>
  
<p-button 
    (click)="decision()" 
    label="Buy GfG Course">
</p-button>
  
<p-messages 
    [value]="ToastMessages">
</p-messages>


app.component.html




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',
  styles: [],
  providers: [ConfirmationService],
})
  
export class AppComponent {
  ToastMessages: Message[];
  
  constructor(
    private confirmationService: ConfirmationService,
    private primengConfig: PrimeNGConfig
  ) {}
  
  decision() {
    this.confirmationService.confirm({
      message: 'Confirm Buying this course?',
      header: 'GeeksforGeeks',
      icon: 'pi pi-exclamation-triangle',
      accept: () => {
        this.ToastMessages = [
          {
            severity: 'success',
            summary: 'Success',
            detail: 'You have bought this course!',
          },
        ];
      },
      reject: () => {
        this.ToastMessages = [
          {
            severity: 'info',
            summary: 'Rejected',
            detail: 'You have rejected bought this course!',
          },
        ];
      },
    });
  }
}


app.module.ts




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 { MessagesModule } from 'primeng/messages';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
  
@NgModule({
  imports: [
    BrowserModule,
    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 Basic using the delete button.

app.component.html




<h2 style="color: green">GeeksforGeeks</h2>
<h5>Angular PrimeNG ConfirmDialog Basic</h5>
  
<p-confirmDialog 
    [style]="{ width: '30vw' }">
</p-confirmDialog>
  
<p-button 
    (click)="decision()" 
    icon="pi pi-times" 
    label="Delete GfG course ?">
</p-button>
  
<p-messages 
    [value]="ToastMessages">
</p-messages>


app.component.ts




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',
  styles: [],
  providers: [ConfirmationService],
})
export class AppComponent {
  ToastMessages: Message[];
  
  constructor(
    private confirmationService: ConfirmationService,
    private primengConfig: PrimeNGConfig
  ) {}
  
  decision() {
    this.confirmationService.confirm({
      message: 'Confirm deleting this course?',
      header: 'GeeksfoGeeks',
      icon: 'pi pi-info-circle',
      accept: () => {
        this.ToastMessages = [
          {
            severity: 'success',
            summary: 'Success!',
            detail: 'You have deleted this course!',
          },
        ];
      },
      reject: () => {
        this.ToastMessages = [
          {
            severity: 'info',
            summary: 'Fail!',
            detail: 'You have not deleted this course!',
          },
        ];
      },
    });
  }
}


app.module.ts




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 { MessagesModule } from 'primeng/messages';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
  
@NgModule({
  imports: [
    BrowserModule,
    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