·10 min read

How to Create Popup in Angular in 4 Different Ways?

Ece Sanan
-Published on:
Jan 19, 2024
-Updated on:
Mar 15, 2024

Angular's modular structure and ready-made components make it easy to create popups.

Using popups in Angular projects is a great way to improve the user experience through better interactivity!

This is one of the reasons why many website developers choose Angular.

In this blog, We will explain three ways to create a popup in Angular and finish with the simplest no-code way.

What is Popup in Angular?

In Angular, a “popup” is like a small window suddenly appearing on your web page.

Angular makes it easy to create popups through libraries that offer pre-designed components and tools.

With Angular, you can create various types of popups as follows.

Modal Dialogues: You can use Angular Material's ‘MatDialog’ module.

mat dialog overview in the components section of the angular material website

They are popups that overlay the main content of a page.

Toasts/Notifications: You can use Angular Material's ‘MatSnackBar’ component.

It is a type of popup that appears briefly to show messages or warnings and disappears after.

Popups/ToolTips: You can use Angular's ngIf, ngClass, and event bindings.

basic tooltip overview in the components section of the angular material website

They are small pop-ups that provide additional information or context when the user hovers over a button or link.

Drop-down Menus: You can use Angular directives (e.g., ngIf, ngFor) and custom components.

Accordions: You can use Angular directives and custom components.

Side Panels: You can create them by defining a separate route in Angular's routing configuration.

4 Ways to Create Popup in Angular

angular website page which contain variety of material components

Check out these step-by-step instructions on how to create popups using Angular Materials, Ng-bootstrap, PrimeNG, and Popupsmart:

Among these methods, you can choose the one that best suits your flexibility and customization needs:

1. Creating a Popup in Angular With Angular Materials

To create a popup in Angular using Angular Materials, you can use the MatDialog module.

Step 1: Install Angular Materials.

Open your terminal and run the following command:

ng add @angular/material

Step 2: Create a popup component.

You can use the Angular CLI to generate it:

ng generate component popup

Step 3: Define the popup content.

Edit the ‘popup.component.html’ file to define the content of your popup:

<h1 mat-dialog-title>Popup Dialog</h1>
<div mat-dialog-content>
  <p>This is a popup dialog created using Angular Material.</p>
</div>
<div mat-dialog-actions>
  <button mat-button (click)="closeDialog()">Close</button>
</div>

Step 4: Create a service

Create a service to handle the opening and closing of the popup.

ng generate service popup/popup

Step 5: Edit the ‘popup.service.ts’ file.

import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { PopupComponent } from './popup.component';

@Injectable({
  providedIn: 'root',
})
export class PopupService {
  constructor(private dialog: MatDialog) {}

  openPopup() {
    this.dialog.open(PopupComponent);
  }
}

Step 6: Use the popup service in your component.

Import the PopupService and use it to open the popup when a button is clicked.

import { Component } from '@angular/core';
import { PopupService } from './popup/popup.service';

@Component({
  selector: 'app-root',
  template: `
    <button (click)="openPopup()">Open Popup</button>
  `,
})
export class AppComponent {
  constructor(private popupService: PopupService) {}

  openPopup() {
    this.popupService.openPopup();
  }
}

Step 7: Import the MatDialog Module.

@NgModule({
  declarations: [...],
  imports: [MatDialogModule, ...],
  providers: [...],
})

Last Step: Run your popup.

2. Creating a Popup in Angular With Ng-bootstrap

image of angular powered bootstrap landing-page

To create a popup in Angular using Ng-bootstrap, you can use the NgbModal module.

Step 1: Install Ng-bootstrap.

ng add @ng-bootstrap/ng-bootstrap

Step 2: Generate a component for your popup.

ng generate component my-popup

Step 3: Import NgbModal from Ng-bootstrap.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

@NgModule({
  declarations: [
    // ...
  ],
  imports: [
    BrowserModule,
    NgbModule, // Add NgbModule here
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }

Step 4: Create a component for the popup.

ng generate component popup-content

Step 5: Open the popup component.

<div>
  <h1>Popup Content</h1>
  <p>This is the content of the popup.</p>
</div>

Step 6: Create a button to trigger the popup.

<button (click)="onButtonClicked()">Click me</button>

Step 7: Create the popup functionality.

import { Component } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { PopupContentComponent } from './popup-content/popup-content.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private modalService: NgbModal) {}

  openPopup() {
    const modalRef = this.modalService.open(PopupContentComponent);
  }
}

Last Step: Run your popup.

ng serve

3. Creating a Popup in Angular With PrimeNG

a image of primeng angular ui companents page

To create a popup in Angular with PrimeNG, you can use the p-dialog component.

Step 1: Install PrimeNG and its required dependencies.

npm install primeng primeicons

Step 2: Import PrimeNG Styles.

"styles": [
  "node_modules/primeicons/primeicons.css",
  "node_modules/primeng/resources/primeng.min.css",
  "node_modules/primeng/resources/themes/saga-blue/theme.css",
  // Other styles...
],

Step 3: Import DialogModule.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { DialogModule } from 'primeng/dialog';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, BrowserAnimationsModule, DialogModule],
  bootstrap: [AppComponent],
})
export class AppModule {}

Step 4: Create the popup component.

Import { Component } from '@angular/core';

@Component({
  selector: 'app-popup-content',
  template: `
    <div>
      <h2>Popup Content</h2>
      <p>Enter your information:</p>
      <input type="text" placeholder="Name">
      <button (click)="closePopup()">Close</button>
    </div>
  `,
})
export class PopupContentComponent {
  closePopup() {
    // Implement the close logic, e.g., using PrimeNG DialogRef
  }
}

Step 5: Use the dialog component.

import { Component } from '@angular/core';
import { DialogService } from 'primeng/dynamicdialog';

@Component({
  selector: 'app-root',
  template: `
    <h1>PrimeNG Popup Example</h1>
    <button (click)="showPopup()">Show Popup</button>
  `,
})
export class AppComponent {
  constructor(private dialogService: DialogService) {}

  showPopup() {
    const ref = this.dialogService.open(PopupContentComponent, {
      header: 'Popup Title',
      width: '70%',
      contentStyle: { 'max-height': '500px', 'overflow': 'auto' },
    });

    // You can also subscribe to events like "onClose" or "onHide" here
    // ref.onClose.subscribe(() => {
    //   // Handle the popup close event
    // });
  }
}

Last Step: Run your popup.

ng serve

Note: Creating popups in Angular with various UI libraries like Angular Materials, Ng-bootstrap, and PrimeNG involves nearly identical steps, with minor differences in library-specific language and configuration.

And here is the bonus step 🌟

4. Creating a Popup in Angular with Popupsmart

If you don't have advanced development skills or time to code for creating popups in Angular, it might be wise to use a no-code popup builder like Popupsmart.

After signing up for Popupsmart, you can choose the pop-up that suits your style from the playbook and then publish it on your Angular site. That's it!

popup creation step on popupsmart for angular website

By customizing these popups to fit your brand and target your audience, you can see how your website increases conversions.

We recommend watching the video below to customize your pop-up, style it, and segment your audience with Popupsmart.

Why Choose Angular to Create Popup?

Let's take a look at why you might choose to use Angular to create popups:

  • Component-Based Architecture: Angular allows you to create pop-up windows that you can use in different parts of your application and make reusable.
  • Two-Way Data Binding: When something changes in your popups, Angular makes sure the screen updates correctly.
  • Dependency Injection: Angular quickly provides the tools and data your popups need to run.
  • Reactive Forms: If your popups have forms, Angular makes it easy to manage them by checking and submitting data.
  • Testing Capabilities: You can quickly test your popups with Angular to make sure they are working correctly.

Wrapping Up

To sum up, we've compiled four different ways, each offering its unique library, from Angular Materials to Ng-bootstrap and PrimeNG.

We've also included Popupsmart, a code-free popup builder for those with limited coding skills.

Whether you're an experienced developer or a beginner, Angular offers a variety of options to improve your website's interactivity and overall user experience.

Frequently Asked Questions

a business woman writing codes on computer

Can I Create Custom Animations for Popups in Angular?

Yes, you can, first, you need to set up Angular animations in your project by importing BrowserAnimationsModule into your app.module.ts file.

Then add effects to your animation using the @angular/animations package.

Apply the animation to your popup element in your component's template using the [@animationName] syntax.

To control when the animation plays, use Angular functions like ngIf or ngClass to change the animation state.

Also, with Popupsmart, you can easily create animated popups without implementing Angular animations manually.

Here's a document on how to do it: How to Add Lottie's Animation to a Popup?

What is the Difference Between a Modal Dialog and a Popup in Angular?

In Angular and web development, the terms "modal dialog" and "popup" are often used as synonyms.

But they can have slight differences, depending on the context.

Modal dialog boxes are like unique pop-ups that prevent you from doing anything else on the web page until you interact with them.

The term “pop-up” is more of a general term that includes many different things on a web page.

How Can I Track User Interactions with Popups in Angular?

You can use event listeners and website analytics tools to monitor user interactions with popups in Angular.

Trigger events that record those actions when a user interacts with the popup.

You can then use Google Analytics to collect and analyze the data.

In addition to event listeners and website analytics tools, popup builders like Popupsmart offer built-in analytics data.

You can easily monitor users' interactions with your popups and gather insights directly from the Popupsmart platform.

This will help you track how users interact with your pop-ups, such as conversion rates and which popups are most effective.

Check These Out!