Skip to content

Instantly share code, notes, and snippets.

@Ze1598
Created December 28, 2019 17:33
Show Gist options
  • Save Ze1598/1510dba4364ccc71e52390f594203f32 to your computer and use it in GitHub Desktop.
Save Ze1598/1510dba4364ccc71e52390f594203f32 to your computer and use it in GitHub Desktop.
Reusable modal component: app.component.ts (final version)
import { Component } from '@angular/core';
import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
import { ModalComponent } from './components/modal/modal.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(public matDialog: MatDialog) { }
openLogoutModal() {
const userId = "user01";
const dialogConfig = new MatDialogConfig();
// The user can't close the dialog by clicking outside its body
dialogConfig.disableClose = true;
dialogConfig.id = "modal-component";
dialogConfig.height = "350px";
dialogConfig.width = "600px";
dialogConfig.data = {
name: "logout",
title: "Are you sure you want to logout?",
description: "Pretend this is a convincing argument on why you shouldn't logout :)",
actionButtonText: "Logout",
userId: userId
}
// https://material.angular.io/components/dialog/overview
const modalDialog = this.matDialog.open(ModalComponent, dialogConfig);
}
openDeleteProductModal() {
const productId = "prod01";
const dialogConfig = new MatDialogConfig();
// The user can't close the dialog by clicking outside its body
dialogConfig.disableClose = true;
dialogConfig.id = "modal-component";
dialogConfig.height = "350px";
dialogConfig.width = "600px";
dialogConfig.data = {
name: "deleteProduct",
title: "Are you sure you want to delete this product?",
description: "If you continue, the product with ID " + productId + " will be deleted.",
actionButtonText: "Delete",
productId: productId
}
// https://material.angular.io/components/dialog/overview
const modalDialog = this.matDialog.open(ModalComponent, dialogConfig);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment