Open In App

Angular PrimeNG ConfirmDialog ConfirmationService

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

The ConfirmDialog Component is used to make a dialog box containing confirm button to confirm the element. The ConfirmationService can be implemented to display the dialog box for the confirmation object options, or hide the dialog box without invoking accept or reject the callbacks.

Angular PrimeNG ConfirmDialog ConfirmationService:

  • confirm: It displays the dialog with the help of the confirmation object options.
  • close: It is used to hide the dialog without invoking accept or reject callbacks.

 

Creating Angular Application And Installing Module:

Step 1: Create an Angular application using the following command.

ng new gfg

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd gfg

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure:  It will look like the following:

 

Example 1: This example illustrates the basic implementation of the ConfirmDialog ConfirmationService in Angular PrimeNG, where we have implemented the confirmed service.

  • app.component.html

HTML




<h2>
    GeeksforGeeks
</h2>
<h5>
    PrimeNG ConfirmDialog ConfimationService
</h5>
<p-confirmDialog [style]="{width: '40vw'}"></p-confirmDialog>
<p-button (click)="Confirm()" 
           label="Open">
</p-button>


  • app.module.ts

Javascript




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 { ConfirmDialogModule } 
    from 'primeng/confirmdialog';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ConfirmDialogModule,
        ButtonModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule { }


  • app.component.ts

Javascript




import { Component } from '@angular/core';
import { ConfirmationService } from 'primeng/api';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [],
    providers: [ConfirmationService]
})
export class AppComponent {
  
    constructor(private confirmationService: ConfirmationService,
        private primengConfig: PrimeNGConfig) { }
  
    Confirm() {
        this.confirmationService.confirm({
            message: 'Angular PrimeNG ConfirmDialog Component',
            header: 'GeeksforGeeks',
        });
  
    }
}


Output:

 

Example 2: This is another example that illustrates the implementation of the ConfirmDialog ConfirmationService in Angular PrimeNG, where we have demonstrates the close service. 

  • app.component.html

HTML




<h2>GeeksforGeeks</h2>
<h5>
    PrimeNG ConfirmDialog ConfimationService
</h5>
<p-confirmDialog [style]="{width: '40vw'}"></p-confirmDialog>
<p-button (click)="Close()" 
          label="Close">
</p-button>


  • app.module.ts

Javascript




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 { ConfirmDialogModule } 
    from 'primeng/confirmdialog';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ConfirmDialogModule,
        ButtonModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule { }


  • app.component.ts

Javascript




import { Component } from '@angular/core';
import { ConfirmationService } from 'primeng/api';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [],
    providers: [ConfirmationService]
})
export class AppComponent {
  
    constructor(private confirmationService: ConfirmationService,
        private primengConfig: PrimeNGConfig) { }
  
  
    Confirm() {
        this.confirmationService.confirm({
            message: 'Do you want to close this dialog?',
            accept: () => {
                console.log('Dialog closed');
            }
        });
    }
  
    Close() {
        this.confirmationService.close();
    }
}


Output:

 

Note: The close() method of the ConfirmationService doesn’t trigger any callbacks, so you won’t see any output when you click the “Close” button as the close() method is used to programmatically close the confirmation dialog.

Reference: https://primeng.org/confirmdialog#api.service



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

Similar Reads