How to open a modal popup on button click in Angular 8

0 votes

How to open a modal popup on button click in Angular 8?

I'm trying to open a modal popup when a button is clicked in my Angular 8 application. Can someone guide me on how to implement this?

Dec 13, 2024 in Web Development by Nidhi
• 11,360 points
73 views

1 answer to this question.

0 votes

1. Install Angular Material (Optional)

If you want to use Angular Material for modals:

ng add @angular/material

2. Create a Modal Component

Generate a new component for the modal:

ng generate component modal

3. Add Modal Logic in the Modal Component

In modal.component.html:

<h2>Modal Content</h2>

<p>This is a modal popup!</p>

<button (click)="close()">Close</button>

In modal.component.ts:

import { Component, Inject } from '@angular/core';

import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';

@Component({

  selector: 'app-modal',

  templateUrl: './modal.component.html',

})

export class ModalComponent {

  constructor(public dialogRef: MatDialogRef<ModalComponent>) {}

  close(): void {

    this.dialogRef.close();

  }

}

4. Open Modal on Button Click

In your main component (app.component.ts or others):

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

import { MatDialog } from '@angular/material/dialog';

import { ModalComponent } from './modal/modal.component';

@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

})

export class AppComponent {

  constructor(public dialog: MatDialog) {}


  openModal(): void {

    this.dialog.open(ModalComponent, {

      width: '300px',

      data: { message: 'This is a modal popup!' },

    });

  }

}

5. Add Button to Open Modal

In app.component.html:

<button (click)="openModal()">Open Modal</button>

6. Import MatDialogModule

Add MatDialogModule to your AppModule:

import { MatDialogModule } from '@angular/material/dialog';

@NgModule({

  declarations: [AppComponent, ModalComponent],

  imports: [BrowserModule, MatDialogModule],

  providers: [],

  bootstrap: [AppComponent],

  entryComponents: [ModalComponent],

})

export class AppModule {}

 Use Information{

  • Create a new component for the Modal using ng generate component Angular_Modal

  • Draw the template:

<div class="modal-overlay" (click)="closeModal()">

  <div class="modal-content" (click)="$event.stopPropagation()">

    <h2>Pop-up Modal</h2>

    <p>Hi Welcome to Edureka</p>

    <button (click)="closeModal()">Close</button>

  </div>

</div>
  • Add logic related to modal in relevant component: 

    export class Angular_ModalComponent {

          @Output() close = new EventEmitter<void>();

          closeModal(): void {

            this.close.emit();

          }

}

}
  • Add the Modal into a AppComponent Template: 

        <button (click)="show()">Show Modal</button>

<app-angular-modal *ngIf="isVisible" (close)="hide()"></app-angular-modal>
  • Update the logic in AppComponent:

export class AppComponent {

  isVisible= false;

  show() {

    this.isVisible = true;

  }

  hide() {

    this.isVisible = false;

  }

}

answered Dec 13, 2024 by Navya

Related Questions In Web Development

0 votes
1 answer

How to verify links that open in a new tab in phpUnit?

Hey @Marium, What do you mean by "the ...READ MORE

answered Dec 14, 2020 in Web Development by Gitika
• 65,770 points
1,416 views
0 votes
1 answer

How to update Angular version in a project?

Angular is a powerful framework for building ...READ MORE

answered Nov 4, 2024 in Web Development by kavya
244 views
0 votes
1 answer

How can you handle a click event on a dynamically created element in jQuery?

To handle a click event on a ...READ MORE

answered Nov 6, 2024 in Web Development by kavya
166 views
0 votes
1 answer

How to create a service file in Angular?

To create a service file in Angular, ...READ MORE

answered Nov 13, 2024 in Web Development by kavya
120 views
+1 vote
8 answers

How can I implement process.env in Angular 5 environment?

Users do not have access to process.env ...READ MORE

answered Apr 3, 2018 in DevOps & Agile by DareDev
• 6,890 points
13,460 views
0 votes
1 answer
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
2,050 views
+4 votes
9 answers

***IMPORTANT*** AngularJS Interview Questions.

Yes, I agree with Omkar AngularJs is ...READ MORE

answered Mar 17, 2019 in Career Counselling by Sharad
• 180 points
3,944 views
0 votes
1 answer

How to Align a Home Button and Dropdown Menus on the Same Line in a Navigation Bar?

Using CSS Flexbox, you can center a ...READ MORE

answered Dec 12, 2024 in Web Development by Navya
96 views
0 votes
1 answer

How to implement a debounce time in keyup event in Angular 6

To implement a debounce time in keyup ...READ MORE

answered Oct 25, 2024 in Web Development by kavya
172 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP