如何从 MatDialog 获取数据?

axe*_*ven 1 angular-material angular

我正在关注此处的文档。我可以将数据传递给对话框,但我没有从中获取数据。我在 .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 
    });
  }

}
Run Code Online (Sandbox Code Playgroud)

axe*_*ven 6

在我注意到我们可以在 MatDialogRef.close() 上传递数据后,我想通了。

onClose(): void {
    this.dialogRef.close('pass data here');
}
Run Code Online (Sandbox Code Playgroud)

该文档仅提供 onNoClick() 函数,顺便说一下不需要传递任何数据。另一方面, onOkClick() ,我认为应该或多或少像上面的 onClose() 。我不知道他们为什么不将它包含在文档中。