Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Angular PrimeNG ConfirmPopup ConfirmationService

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:

  • confirm: It is used to display the overlay using the confirmation object options.
  • close: It is used to hide the overlay without invoking accept or reject callbacks. 

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.

  • app.component.html:

HTML




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

  • app.component.ts:

Javascript




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

  • app.module.ts:

Javascript




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.

  • app.component.html:

HTML




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

  • app.component.ts:

Javascript




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

  • app.module.ts:

Javascript




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


My Personal Notes arrow_drop_up
Last Updated : 30 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials