Open In App

Angular PrimeNG ConfirmDialog Events

Last Updated : 12 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 Angular PrimeNG ConfirmDialog Events.

 The ConfirmDialog component is used to make a dialog box containing confirm button to confirm the element. Angular PrimeNG provides events, like onHide which is used for Callback to invoke when the dialog is hidden.

Angular PrimeNG ConfirmDialog Events:

  • onHide: This is used for Callback to invoke when the dialog is hidden.

 

Syntax:

<p-confirmDialog (event)=function()>
    ...
</p-confirmDialog>

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:

 

  • Steps to run the application: Run the below command to see the output
ng serve --save

Example 1: Below is the example code that illustrates the use of Angular PrimeNG ConfirmDialog Events, where we will learn about onHide event.

  • app.component.html:

HTML




<div style="width: 80%;">
    <h1 style="color: green;">
          GeeksforGeeks
      </h1>
    <h2>
          Angular PrimeNG ConfirmDialog Events
      </h2>
    <p-confirmDialog [style]="{width: '60vw'}"
                     (onHide)="onHide()">
      </p-confirmDialog>
    <p-button (click)="GetConfirm()" 
              label="Click Here">
      </p-button>
</div>


  • 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
    ) { }
  
    GetConfirm() {
        this.confirmationService.confirm({
            message: "Angular PrimeNG ConfirmDialog Component",
            header: "GeeksforGeeks"
        });
    }
  
    onHide() {
        alert("Hi Geek!! I am closed");
    }
}


  • 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 {}


Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG ConfirmDialog Events, where we will display some text when confirm dialog is closed.

  • app.component.html:

HTML




<div style="width: 80%;">
    <h1 style="color: green;">
          GeeksforGeeks
      </h1>
    <h2>
          Angular PrimeNG ConfirmDialog Events
      </h2>
    <p-confirmDialog [style]="{width: '60vw'}"
                     (onHide)="onHide()">
      </p-confirmDialog>
    <p-button (click)="GetConfirm()" 
              label="Click Here">
      </p-button>
    <h4 style="color: red;">
          {{display}}
      </h4>
</div>


  • 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
    ) { }
  
    display: string = "";
    GetConfirm() {
        this.confirmationService.confirm({
            message: "Angular PrimeNG ConfirmDialog Component",
            header: "GeeksforGeeks"
        });
    }
  
    onHide() {
        this.display = "Confirm Dialog Closed";
    }
}


  • 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 { }


Output:

 

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



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

Similar Reads