data
parameter)import {Component, Inject} from '@angular/core'; import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material"; @Component({ selector: 'app-alert-dialog', templateUrl: './alert-dialog.component.html', styleUrls: ['./alert-dialog.component.css'] }) export class AlertDialogComponent { constructor(public dialogRef: MatDialogRef<AlertDialogComponent>, @Inject(MAT_DIALOG_DATA) public data: any) { console.log(data); } onOKClick(): void { this.dialogRef.close(); } }
<div mat-dialog-content> <p>{{data.message}}</p> </div> <div mat-dialog-actions> <button mat-button style="float: right" class="btn btn-primary"(click)="onOKClick()">OK</button> </div>
...; import {AlertDialogComponent} from "@app/shared/alert-dialog/alert-dialog.component"; export const appConfig = { declarations: [ ..., AlertDialogComponent ], imports: [ ..., ], entryComponents:[ ..., AlertDialogComponent], providers: [...], bootstrap: [AppComponent]
...; import {AlertDialogComponent} from "@app/shared/alert-dialog/alert-dialog.component"; import {MatDialog} from "@angular/material";
constructor(public dialog: MatDialog) {}
const dialogRef = this.dialog.open(AlertDialogComponent, { width: '250px', data: {message: "OK, Jahaa, sehr gut!"} });
date.message
to pass the message to our alert dialog component. Go back to step 3 and 4, you will see how to dialog get that date
.
component that open the dialog
should subscribe the dialog afterClosed event as below. The result
would be what the dialog component pass to the subscriberdialogRef.afterClosed().subscribe(result => {...});
dialog component
, when you close it, close it with a parameter of any kind you like. that would be what the subscriber receives as the result
variable. In the below case, the subscriber will receive a number of 10.this.dialogRef.close(10);