【发布时间】:2023-03-26 23:42:01
【问题描述】:
我正在关注here 中的文档。 我可以将数据传递给对话框,但我没有从中获取数据。 我在 .afterClose().subscribe() 上得到了未定义的结果。 我错过了什么?我猜我需要在对话框的模板中做一些事情,但上面的文档没有提供示例。 这是我的代码:
import {Component, Inject, OnInit} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';
import {MySavior} from '../shared/my-savior';
import {Savior} from '../../savior/shared/savior';
import {SaviorDataService} from '../../savior/shared/savior-data.service';
@Component({
selector: 'app-my-room-savior-select-dialog',
template: 'my data name: {{data.name}}'
})
export class MySaviorSelectDialogComponent {
constructor(public dialogRef: MatDialogRef<MySaviorSelectDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {}
onClose(): void {
this.dialogRef.close();
}
}
@Component({
selector: 'app-my-room-my-savior',
templateUrl: './my-savior.component.html',
styleUrls: ['./my-savior.component.css']
})
export class MySaviorComponent implements OnInit {
saviors: Savior[] = [];
mySaviors: MySavior[] = [];
constructor(private saviorDataServ: SaviorDataService, public dialog: MatDialog) {}
ngOnInit() {
...
}
openSelectDialog(): void {
const dialogRef = this.dialog.open(MySaviorSelectDialogComponent, {data: {name: 'test'}});
dialogRef.afterClosed().subscribe(result => {
console.log('result ' + result); //i got result undefined
});
}
}
【问题讨论】: