如何从 mat dialog 组件到实现 mat dialog 的组件进行通信?

How to communicate from mat dialog component to the component where mat dialog is implemented?

我有一个对话框组件和应用程序组件,其中实现了 material 对话框。 这里是app组件的代码

import { Component } from '@angular/core';
import {VERSION, MatDialog, MatDialogRef} from '@angular/material';
import { DialogComponent } from '../dialog.component';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 5';
  DialogRef: MatDialogRef<DialogComponent>;
  constructor(private dialog: MatDialog) {}
  ngOnInit() {
  }
  addItem() {
    this.DialogRef = this.dialog.open(DialogComponent);
  }

receiveMessageFromDialogComponent() {
  // how to receive message from dialog component
}
closeDialog(): void {
  this.DialogRef.close();
}
}

对话框组件是实现表单的地方,我需要获取表单值并在此处接收。我尝试使用 angular 输入和输出来实现此目的,但由于没有 child 和 parent 通信,所以无法正常工作。 这是对话框组件

import { Component } from '@angular/core';

@Component({
  template: `
    <h1 mat-dialog-title>Add Item</h1>
    <mat-dialog-content>
    <mat-form-field  class="example-full-width">
        <input matInput placeholder="Item name here...">
    </mat-form-field>
    </mat-dialog-content>
    <mat-dialog-actions>
      <button mat-button (click)="saveMessage()">Add</button>
      <button mat-button (click)="closeDialog()">Cancel</button>
    </mat-dialog-actions>
  `
})
export class DialogComponent {
  saveMessage() {
    console.log('how to send data to the app component');
  }
  closeDialog() {
    console.log('how to close');
  }
}

Working Example on StackBlitz

一个。订阅this.DialogRefafterClosed Observable,你可以在调用app.component.ts中的this.DialogRef.open之后添加它,像这样

  addItem() {
    this.DialogRef = this.dialog.open(DialogComponent);
    this.DialogRef.afterClosed()
    .subscribe(return => console.log(return));
  }

乙。在dialog.component.ts导入MatDialog服务,像这样:

import { Component, Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from'@angular/material';

摄氏度。确保像这样将 dialogRef 传递给您的对话框构造函数

  constructor(public dialogRef: MatDialogRef<DialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any) {}

摄氏度。在saveMessage()方法中,调用close dialog方法,将需要return的值传给app组件

  saveMessage() {
    this.dialogRef.close('hello data');
  } 

D. App 组件将收到该值,因为它订阅了 afterClosed 对话框可观察

另请查看完整示例表格 angular material docs

干杯

一个用例场景,如果您想在对话框中编辑一些数据,然后将编辑后的数据从对话框传递回组件。我使用了上面的示例,但我合并了答案以使其更容易理解。假设数据来自服务,此示例将数据从 mat-dialog 组件共享到同一文件中的应用程序组件。

// app.component.html  

<div *ngFor="let a of appData">
  <p>{{a.name}}</p>
<button> (click)="open(event, a)">edit</button> 
</div>
// app.component.ts    

import { Component, Inject, OnInit } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { DataService } from './data.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {
 appData: Array <any>;

 constructor(private dataService: DataService, public dialog: MatDialog) {}

 ngOnInit() {

   this.dataService.getData()
    .subscribe(res => { 
      //console.log(res);
      this.appData = res;
   })
 }

public open(event, data) {
  this.dialog.open(EditDialog, {
    data: data,
}).afterClosed()
  .subscribe(item => {
    // Edited Data sent to App Component from Dialog 
    console.log(item);
  });
 }
}

@Component({
  selector: 'edit-dialog',
  template: `<span>Edit Data</span>
             <mat-dialog-content>
               <input matInput name="title" type="text" class="form-control" placeholder="Edit Name" [(ngModel)]="dataItem.name">
             </mat-dialog-content>
             <div>
               <span><button mat-raised-button (click)="updateData()">Update Recipe</button></span>
             </div>`,
 })

 export class EditDialog implements OnInit {

   dataItem: any;  

   constructor(public dialogRef: MatDialogRef <EditDialog> , @Inject(MAT_DIALOG_DATA) public data: any, private dataService: DataService) {
     this.dataItem = this.data;
   }

   public updateData() {
     this.dialogRef.close(this.dataItem);
   }

   ngOnInit() {
   }
}

您实际上可以通过订阅@Output通过MatDialogRef<DialogComponent>实现通信。对于某些场景,您可能需要在对话框关闭之前从对话框中获取数据。因此,我们不能使用 this.DialogRef.afterClosed() 函数,因为我们必须先关闭对话框才能获取数据。相反,我们想要获取 Component 实例并从那里访问。

在您的 DialogComponent 上:

export class DialogComponent {
 
  @Output() submitClicked = new EventEmitter<any>();

  constructor(public dialogRef: MatDialogRef<DialogComponent>){}

  saveMessage() {
    this.submitClicked.emit('Your data');
  }

  closeDialog() {
    this.dialogRef.close();
  }
}

在你的 AppComponent:

openDialog() {
    this.dialogRef = this.dialog.open(DialogComponent);
    this.dialogRef.componentInstance.submitClicked.subscribe(result => {
        console.log('Got the data!', result);
    });
}

最好确保 unsubscribe() 所有 Subscription。像这样的事情可以快速取消订阅(如果不涉及数据验证):

const dialogSubmitSubscription = this.dialogRef.componentInstance.submitClicked
    .subscribe(result => {
      console.log('Got the data!', result);
      // do something here with the data
      dialogSubmitSubscription.unsubscribe();
    });

此外,如果需要,您可以随时使用 this.dialogRef.close()AppComponent 关闭对话框。或者在上面的例子中,你也可以使用 this.DialogRef.componentInstance.closeDialog().