7

Is there a way in Angular 8 to generate a small dialog without having to create a new Component? Some small message like "Operation completed" that wouldn't require much interaction with the user. I just feel like having 4 more files in a project just to display a success message would be too much:

small-dialog.component.html
small-dialog.component.scss
small-dialog.component.spec.ts
small-dialog.component.ts

Or maybe there's a way to create a "default component" in few lines without having to actually generate a new component?

4
  • 1
    What's your use case for not wanting to create a new component? To me this is the perfect example of when to create a reusable component? Commented Feb 21, 2020 at 14:53
  • 1
    How exactly do you want to open the dialog? Commented Feb 21, 2020 at 14:53
  • I tried to explain better in my original post, check it out! Commented Feb 21, 2020 at 15:00
  • Answered here! stackoverflow.com/a/67264069/12834967 Commented Apr 26, 2021 at 12:15

1 Answer 1

20

If you don't want to create component for dialog itself you can do something like this:

View.html

<button (click)="openDialog(template)">Open my dialog</button>
<ng-template #template>
 <h1>This is a message</h1>
</ng-template>

Component.ts

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

 
constructor(public dialog: MatDialog) {}

openDialog(templateRef) {
  let dialogRef = this.dialog.open(templateRef, {
   width: '300px'
 });
}

Here is also one example how you can do the same thing.

But I propose to you that you create generic dialog component which you can use through the whole application, how to get started with that you can see it here.

Update: How to create a dialog without Material components can be seen here.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer! How should I define this.dialog? And what if the data I want to display is just a string? Sorry but I'm really a beginner in Angular :)
@Robb1 you can use the overlaymoduke of angular CDK
And without Material ? The question doesn't ask for Material Design to be loaded into the project.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.