简体   繁体   English

如何从MatDialog获取数据?

[英]How to get data from MatDialog?

I am following the documentation in here . 我在这里关注文档。 I can pass data to the dialog but I am not getting data from it. 我可以将数据传递给对话框,但我没有从中获取数据。 I am getting undefined result on .afterClose().subscribe() instead. 我在.afterClose()。subscribe()上获得了未定义的结果。 What am I missing? 我错过了什么? I am guessing there is something I need to do in the dialog's template but the documentation above does not provide an example for it. 我猜我在对话框的模板中需要做些什么但是上面的文档没有提供它的例子。 Here's my code: 这是我的代码:

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 
    });
  }

}

I figure it out after I notice that we can pass data on MatDialogRef.close(). 我注意到我们可以在MatDialogRef.close()上传递数据后想出来。

onClose(): void {
    this.dialogRef.close('pass data here');
}

The documentation only provide onNoClick() function which incidentally does not need to pass any data. 该文档仅提供onNoClick()函数,顺便说一下,它不需要传递任何数据。 The onOkClick() , on the other hand, I think should be more or less like the onClose() above. 另一方面,onOkClick(),我认为应该或多或少像上面的onClose()。 I don't know why they don't include it in the documentation. 我不知道他们为什么不在文档中包含它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM