Angular Popup - Show and Hide the Popup

API

NOTE
In this article, the Button UI component is used to demonstrate how to show and hide the Popup. This choice is made for purely demonstrational purposes, and you can do the same operations using another UI component following the same guidelines.

To show or hide the Popup programmatically, bind the visible property of Popup to a component property. After that, change the latter property, and the Popup will appear or disappear.

HTML
TypeScript
  • <dx-popup
  • title="Popup Title"
  • [(visible)]="isPopupVisible">
  • <div *dxTemplate="let data of 'content'">
  • <p>Popup content</p>
  • </div>
  • </dx-popup>
  • <dx-button
  • text="Show the Popup"
  • (onClick)="isPopupVisible = true">
  • </dx-button>
  • <dx-button
  • text="Hide the Popup"
  • (onClick)="isPopupVisible = false">
  • </dx-button>
  • import { DxPopupModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • isPopupVisible: boolean = false;
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxPopupModule
  • ],
  • // ...
  • })

Using DxPopupService

DevExtreme Popup includes a DxPopupService that allows you to initiate popups directly from .ts files without template code. The use of services that display popups is a common practice in Angular libraries.

DxPopupService is imported from 'devextreme-angular/ui/popup' and functions as a typical Angular service. To show a Popup with DxPopupService, call the open method. The method takes two arguments:

  1. The Angular component class to be used as the content of the popup.
  2. The popup configuration, which includes properties of the Popup component.

The open method returns a DxPopupServiceComponent object. This type extends DxPopupComponent with a contentRef property. Use contentRef.instance to access the component that serves as popup content.

app.component.ts
employee-info.component.ts
  • import { DxPopupService } from 'devextreme-angular/ui/popup';
  • // ...
  • export class AppComponent {
  • employees: Employee[];
  •  
  • constructor(
  • employeeService: EmployeeService,
  • private popupService: DxPopupService
  • ) {
  • this.employees = employeeService.getEmployees();
  • }
  •  
  • showInfo(employee: Employee) {
  • const popupRef = this.popupService.open(EmployeeInfoComponent, {
  • showTitle: true,
  • title: 'Information',
  • container: 'html',
  • width: 300,
  • showCloseButton: true,
  • });
  •  
  • popupRef.contentRef.instance.currentEmployee = employee;
  • }
  • }
  • import { Component, Input } from '@angular/core';
  • @Component({
  • standalone: true,
  • selector: '',
  • template: `<div>
  • <p>
  • Full Name:
  • <span>
  • {{ currentEmployee['FirstName'] }}
  • {{ currentEmployee['LastName'] }}
  • </span>
  • </p>
  • <p>
  • Birth Date:
  • <span>{{ currentEmployee['BirthDate'] }}</span>
  • </p>
  • <p>
  • Address:
  • <span>{{ currentEmployee['Address'] }}</span>
  • </p>
  • <p>
  • Hire Date:
  • <span>{{ currentEmployee['HireDate'] }}</span>
  • </p>
  • <p>
  • Position:
  • <span>{{ currentEmployee['Position'] }}</span>
  • </p>
  • </div>`,
  • })
  •  
  • export class EmployeeInfoComponent {
  • @Input() currentEmployee: Record<string, any> = {};
  • }

You can access the Popup instance through DxPopupServiceComponent. Call the hide() method to close the Popup programmatically:

app.component.ts
  • import { DxPopupService, DxPopupServiceComponent } from 'devextreme-angular/ui/popup';
  • // ...
  • export class AppComponent {
  • employees: Employee[];
  • popupRef!: DxPopupServiceComponent;
  •  
  • constructor(
  • employeeService: EmployeeService,
  • private popupService: DxPopupService
  • ) {
  • this.employees = employeeService.getEmployees();
  • }
  •  
  • showInfo(employee: Employee) {
  • this.popupRef = this.popupService.open(EmployeeInfoComponent, {
  • showTitle: true,
  • title: 'Information',
  • container: 'html',
  • width: 300
  • });
  •  
  • this.popupRef.contentRef.instance.currentEmployee = employee;
  • }
  •  
  • closePopup() {
  • this.popupRef.instance.hide();
  • }
  • }

User Interaction

Enable the showCloseButton property to allow a user to hide the Popup component by clicking the Close button.

HTML
  • <dx-popup
  • [showTitle]="true"
  • [showCloseButton]="true">
  • </dx-popup>

The Popup can also be hidden when a user clicks outside of it. To control this Popup behavior, use the hideOnOutsideClick property.

HTML
TypeScript
  • <dx-popup
  • title="Popup Title"
  • [(visible)]="isPopupVisible"
  • [hideOnOutsideClick]="true">
  • </dx-popup>
  • import { DxPopupModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • isPopupVisible: boolean = true;
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxPopupModule
  • ],
  • // ...
  • })

You can also implement a custom Close button inside the Popup. Refer to the following demo for more information:

View Demo

Events

To execute certain commands before or after the Popup was shown/hidden, handle the showing, shown, hiding or hidden event. If the event handling function is not going to be changed during the lifetime of the UI component, assign it to the corresponding onEventName property when you configure the UI component.

HTML
TypeScript
  • <dx-popup ...
  • (onShowing)="popup_showing($event)"
  • (onShown)="popup_shown($event)"
  • (onHiding)="popup_hiding($event)"
  • (onHidden)="popup_hidden($event)">
  • </dx-popup>
  • import { DxPopupModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • popup_showing (e) {
  • // Handler of the "showing" event
  • }
  • popup_shown (e) {
  • // Handler of the "shown" event
  • }
  • popup_hiding (e) {
  • // Handler of the "hiding" event
  • }
  • popup_hidden (e) {
  • // Handler of the "hidden" event
  • }
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxPopupModule
  • ],
  • // ...
  • })
See Also