- Alert dialog
- Confirm dialog
- Close utility dialog
Contact Support
Render a dialog using utility functions in Angular Dialog component
27 Apr 202413 minutes to read
The dialog component provides built-in utility functions to render the alert and confirm dialogs with the minimal code.
The following options are used as an argument on calling the utility functions:
Options | Description |
---|---|
title | Specifies the title of dialog like the header property. |
content | Specifies the value that can be displayed in dialog’s content area like the content property. |
isModal | Specifies the Boolean value whether the dialog can be displayed as modal or non-modal. For more details, refer to the isModal property. |
position | Specifies the value where the alert or confirm dialog is positioned within the document. For more details, refer to the position property { X: ‘center’, Y: ‘center’} |
okButton | Configures the OK button that contains button properties with the click events. okButton:{ icon:'prefix icon to the button', cssClass:'custom class to the button', click: 'action for OK button click', text: 'Yes' // <-- Default value is 'OK' }
|
cancelButton | Configures the Cancel button that contains button properties with the click events. cancelButton:{ icon:'prefix icon to the button', cssClass:'custom class to the button', click: 'action for ‘Cancel’ button click', text: 'No' // <-- Default value is 'Cancel'}
|
isDraggable | Specifies the value whether the alert or confirm dialog can be dragged by the user. |
showCloseIcon | When set to true, the close icon is shown in the dialog component. |
closeOnEscape | When set to true, you can close the dialog by pressing ESC key. |
animationSettings | Specifies the animation settings of the dialog component. |
cssClass | Specifies the CSS class name that can be appended to the dialog. |
zIndex | Specifies the order of the dialog, that is displayed in front or behind of another component. |
open | Event which is triggered after the dialog is opened. |
Close | Event which is triggered after the dialog is closed. |
Alert dialog
An alert dialog box is used to display warning like messages to the users. Use the following code to render a simple alert dialog in an application.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { Component, ViewChild, OnInit, ViewEncapsulation } from '@angular/core';
import { DialogUtility } from '@syncfusion/ej2-popups';
@Component({
imports: [
],
standalone: true,
selector: 'app-root',
template: `<button class="e-control e-btn" id="targetButton" (click)="onOpenDialog($event)">Open Alert Dialog</button>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent implements OnInit {
ngOnInit(): void {
throw new Error('Method not implemented.');
}
public onOpenDialog = function(event: any): void {
DialogUtility.alert('This is an Alert Dialog!');
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Render an alert dialog with options
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { Component, ViewChild, OnInit } from '@angular/core';
import { DialogUtility } from '@syncfusion/ej2-popups';
@Component({
imports: [
],
standalone: true,
selector: 'app-root',
template: `<button class="e-control e-btn" id="targetButton" (click)="onOpenDialog($event)">Open Alert Dialog</button>`
})
export class AppComponent implements OnInit {
ngOnInit(): void {
throw new Error('Method not implemented.');
}
public onOpenDialog = (event: any): void => {
DialogUtility.alert({
title: 'Alert Dialog',
content: "This is an Alert Dialog!",
okButton: { text: 'OK', click: this.okClick.bind(this) },
showCloseIcon: true,
closeOnEscape: true,
animationSettings: { effect: 'Zoom' }
});
}
private okClick(): void {
alert('you clicked OK button');
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Confirm dialog
A confirm dialog displays a specified message along with ‘OK’ and ‘Cancel’ button.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { Component, ViewChild, OnInit } from '@angular/core';
import { DialogUtility } from '@syncfusion/ej2-popups';
@Component({
imports: [
],
standalone: true,
selector: 'app-root',
template: `<button class="e-control e-btn" id="targetButton" (click)="onOpenDialog($event)">Open Confirm Dialog</button>`
})
export class AppComponent implements OnInit {
ngOnInit(): void {
throw new Error('Method not implemented.');
}
public onOpenDialog = function(event: any): void {
DialogUtility.confirm('This is a Confirmation Dialog!');
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Render a confirmation dialog with options
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { Component, ViewChild, OnInit } from '@angular/core';
import { DialogUtility } from '@syncfusion/ej2-popups';
@Component({
imports: [
],
standalone: true,
selector: 'app-root',
template: `<button class="e-control e-btn" id="targetButton" (click)="onOpenDialog($event)">Open Confirm Dialog</button>`
})
export class AppComponent implements OnInit {
ngOnInit(): void {
throw new Error('Method not implemented.');
}
public onOpenDialog = (event: any): void => {
DialogUtility.confirm({
title: ' Confirmation Dialog',
content: "This is a Confirmation Dialog!",
okButton: { text: 'OK', click: this.okClick.bind(this) },
cancelButton: { text: 'Cancel', click: this.cancelClick.bind(this) },
showCloseIcon: true,
closeOnEscape: true,
animationSettings: { effect: 'Zoom' }
});
}
private okClick(): void {
alert('you clicked OK button');
}
private cancelClick(): void {
alert('you clicked Cancel button');
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Close utility dialog
When rendering an Alert and Confirmation dialog through utility methods, You can close the dialog using the following ways.
- By pressing the escape key if the closeOnEscape property is enabled.
- By clicking the close button if the showCloseIcon property is enabled.
You can also manually close the Dialogs by creating an instance to the dialog and call the hide method.
Below sample demonstrates the different ways of hiding the utility dialog.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { Component, ViewChild, OnInit } from '@angular/core';
import { DialogUtility } from '@syncfusion/ej2-popups';
@Component({
imports: [
],
standalone: true,
selector: 'app-root',
template: `<button class="e-control e-btn" id="targetButton" (click)="onOpenDialog($event)">Open Confirm Dialog</button>`
})
export class AppComponent implements OnInit {
ngOnInit(): void {
throw new Error('Method not implemented.');
}
public DialogObj: any;
public onOpenDialog = (event: any): void => {
this.DialogObj = DialogUtility.confirm({
title: 'Confirmation Dialog',
content: "This is a Confirmation Dialog!",
okButton: { text: 'OK', click: this.okClick.bind(this) },
cancelButton: { text: 'Cancel', click: this.cancelClick.bind(this) },
showCloseIcon: true,
closeOnEscape: true,
animationSettings: { effect: 'Zoom' }
});
}
private okClick(): void {
alert('you clicked OK button');
}
private cancelClick(): void {
//Hide the dialog
this.DialogObj.hide();
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));