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, call the show() or hide() method. The same thing can be done using the toggle(showing) method. Pass true or false to this method to show or hide the Popup, respectively.

JavaScript
  • $(function() {
  • $("#popupContainer").dxPopup({
  • title: "Popup Title",
  • contentTemplate: function () {
  • return $("<p />").text("Popup content");
  • }
  • });
  • $("#showButton").dxButton({
  • text: "Show the Popup",
  • onClick: function () {
  • $("#popupContainer").dxPopup("show");
  • // === or ===
  • $("#popupContainer").dxPopup("toggle", true);
  • }
  • });
  • $("#hideButton").dxButton({
  • text: "Hide the Popup",
  • onClick: function () {
  • $("#popupContainer").dxPopup("hide");
  • // === or ===
  • $("#popupContainer").dxPopup("toggle", false);
  • }
  • });
  • });

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.

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

User Interaction

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

JavaScript
  • $(function() {
  • $("#popupContainer").dxPopup({
  • showTitle: true,
  • showCloseButton: true
  • });
  • });

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

JavaScript
  • $(function() {
  • $("#popupContainer").dxPopup({
  • title: "Popup Title",
  • visible: true,
  • hideOnOutsideClick: true
  • });
  • });

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.

JavaScript
  • $(function () {
  • $("#popupContainer").dxPopup({
  • // ...
  • onShowing: function (e) {
  • // Handler of the "showing" event
  • },
  • onShown: function (e) {
  • // Handler of the "shown" event
  • },
  • onHiding: function (e) {
  • // Handler of the "hiding" event
  • },
  • onHidden: function (e) {
  • // Handler of the "hidden" event
  • }
  • });
  • });

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method.

JavaScript
  • const hiddenEventHandler1 = function (e) {
  • // First handler of the "hidden" event
  • };
  •  
  • const hiddenEventHandler2 = function (e) {
  • // Second handler of the "hidden" event
  • };
  •  
  • $("#popupContainer").dxPopup("instance")
  • .on("hidden", hiddenEventHandler1)
  • .on("hidden", hiddenEventHandler2);
See Also