Przykładowy kod wywołujący okno dialogowe z przekazaniem zmiennej:
import { Component, Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent {
imie: string = "Jan";
constructor(public dialog: MatDialog) { }
openDialog(): void {
const dialogRef = this.dialog.open(DialogExample, {
width: '250px',
data: { name: this.imie }
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
});
}
}
@Component({
selector: 'dialog-example',
templateUrl: 'dialog-example.html',
})
export class DialogExample {
constructor(
public dialogRef: MatDialogRef<DialogExample>,
@Inject(MAT_DIALOG_DATA) public data: any) { }
}
W pliku HTML komponentu Example należy dodać przycisk, który wywoła funkcję otwierającą okno dialogowe:
<button mat-raised-button (click)="openDialog()">Pokaz imie</button>
W pliku HTML dialogu należy umieścić przekazywaną zmienną:
<h2>Twoje imie to: {{ data.name }}</h2>