Open In App

Angular PrimeNG ConfirmPopup ConfirmationService

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 learn how to use the ConfirmPopup Component in Angular PrimeNG.

The ConfirmPopup Component is used as a confirmation overlay and is shown relative to the target.



Import: In order to use the ConfirmPopup, we need to import the following statement in the module file:

import {ConfirmPopupModule} from 'primeng/confirmpopup';
import {ConfirmationService} from 'primeng/api';

 



Angular PrimeNG ConfirmPopup Component ConfirmationService:

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:

 

Step to run the application: Run the below command to see the output

ng serve --save

Example 1: In the below code, we will make use of the above syntax to demonstrate the use of the ConfirmPopup ConfirmationService.




<div style="text-align:center">
    <h1 style="color:green">
          GeeksforGeeks
    </h1>
    <h5>Angular PrimeNG ConfirmPopup ConfirmationService</h5>
    <p-confirmPopup></p-confirmPopup>
    <button (click)="confirm($event)" 
          pButton label="Confirm">
    </button
</div>




import { Component } from "@angular/core";
import {
    ConfirmationService,
    MessageService,
    PrimeNGConfig
} from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {
    constructor(
        private confirmationService: ConfirmationService,
        private messageService: MessageService,
        private primengConfig: PrimeNGConfig
    ) {}
  
    confirm(event: Event) {
        this.confirmationService.confirm({
            target: event.target,
            message: "Are you sure that you want to proceed?",
            icon: "pi pi-exclamation-triangle",
            accept: () => {
                this.messageService.add({
                    severity: "info",
                    summary: "Confirmed",
                    detail: "You have accepted"
                });
            },
            reject: () => {
                this.messageService.add({
                    severity: "error",
                    summary: "Rejected",
                    detail: "You have rejected"
                });
            }
        });
    }
  
    ngOnInit() {
        this.primengConfig.ripple = true;
    }
}




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ConfirmPopupModule } from "primeng/confirmpopup";
import { ToastModule } from "primeng/toast";
import { ButtonModule } from "primeng/button";
import { ConfirmationService, MessageService } from "primeng/api";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        FormsModule,
        ConfirmPopupModule,
        ToastModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ConfirmationService, MessageService]
})
  
export class AppModule {}

Output:

 

Example 2: In the below code, we will make use of the above syntax to demonstrate the use of the ConfirmPopup ConfirmationService.




<div style="text-align:center">
  <h1 style="color:green">GeeksforGeeks</h1>
  <h5>Angular PrimeNG ConfirmPopup ConfirmationService</h5>
  <p-confirmPopup></p-confirmPopup>
  <button 
        (click)="confirm($event)" 
        pButton label="Confirm">
  </button
  <button 
         (click)="cancel($event)" 
         pButton label="cancel">
  </button>
</div>




import { Component } from "@angular/core";
import {
    ConfirmationService,
    MessageService,
    PrimeNGConfig
} from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {
    constructor(
        private confirmationService: ConfirmationService,
        private messageService: MessageService,
        private primengConfig: PrimeNGConfig
    ) {}
  
    confirm(event: Event) {
        this.confirmationService.confirm({
            target: event.target,
            message: "Are you sure that you want to proceed?",
            icon: "pi pi-exclamation-triangle",
            accept: () => {
                this.messageService.add({
                    severity: "info",
                    summary: "Confirmed",
                    detail: "You have accepted"
                });
            },
            reject: () => {
                this.messageService.add({
                    severity: "error",
                    summary: "Rejected",
                    detail: "You have rejected"
                });
            }
        });
    }
  
    ngOnInit() {
        this.primengConfig.ripple = true;
    }
}




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ConfirmPopupModule } from "primeng/confirmpopup";
import { ToastModule } from "primeng/toast";
import { ButtonModule } from "primeng/button";
import { ConfirmationService, MessageService } from "primeng/api";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        FormsModule,
        ConfirmPopupModule,
        ToastModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ConfirmationService, MessageService]
})
  
export class AppModule {}

Output:

 

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


Article Tags :