首页 > 解决方案 > 如何访问有角材料弹出/对话框组件内的组件属性?

问题描述

我正在从我的组件中打开一个对话框。

 const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
             width: '500px',
            data: DialogData
            });

    dialogRef.afterClosed().subscribe(result => {
        this.dialogData = new DialogData('label...',this.frequency,'action','screen_ocr','parameter_type','parameter_value',0,'next_node',1,'index',false);
      console.log('The dialog was closed');

    });

我想从这个组件访问几个属性来初始化 DialogOverviewExampleDialog

@Component({
  selector: 'dialog-overview-example-dialog',
  templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {

    //here I want to use properties of my component

  constructor(private dialogService: DialogDataService,
    public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
    @Inject(MAT_DIALOG_DATA) public data: DialogData) {}

唯一的方法,我可以弄清楚这是使用 rxjs 的 behaviorsubject 来访问属性,但是这个 DialogOverviewExampleDialog 是在我的组件内部声明的。有没有办法在 DialogOverviewExampleDialog 中直接访问我的组件的属性?反之亦然。

谢谢,

标签: angularangular-materialangular-material-6angular-material-7

解决方案


用这个:dialogRef.componentInstance.YOUR_PROPERTY

    const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
             width: '500px',
            data: DialogData
            });
    dialogRef.componentInstance.YOUR_PROPERTY

    dialogRef.afterClosed().subscribe(result => {
        this.dialogData = new DialogData('label...',this.frequency,'action','screen_ocr','parameter_type','parameter_value',0,'next_node',1,'index',false);
      console.log('The dialog was closed');

    });

推荐阅读