0

I am using mat-dialog of angular material. Here is my code:

if(abc_name === name){
    const dialogRef = this.dialog.open(ViewAbcComponent, {
      panelClass: 'full-screen-dialog-container',
      data: {abc_name: this.abcDetails.item1[keyValue]}
    });
    dialogRef.afterClosed().subscribe((result) => {
      if(result){}
      console.log("ViewAbcComponent dialog closed", result);
    });
  }

In data: object I want tp pass dynamic abc_name value. Any idea how to achieve that?

rk01
  • 76
  • 9
  • Does this answer your question? [How to pass data to dialog of angular material 2](https://stackoverflow.com/questions/42664974/how-to-pass-data-to-dialog-of-angular-material-2) – Aman Gojariya Mar 02 '21 at 11:57
  • No. I want to pass dynamic object key while passing data. – rk01 Mar 02 '21 at 12:01

1 Answers1

0

You can create subject/BehaviorSubject in service and listen (subscribe) to that subject in your ViewAbcComponent component. as soon as your data changed you have to update data as subject.next(data); and you will get it in component. Subject example:

const subject = new Rx.Subject();
subject.next(1);
subject.subscribe(x => console.log(x));

BehaviorSubject example:

const subject = new Rx.BehaviorSubject(0);
subject.next(1);
subject.subscribe(x => console.log(x));

Elaborating : (as per request)

1.You have to add varible in your service lets say app.service.ts and add update method.

const subject = new Rx.Subject();

  updateSubject(data){
   subject.next(data)
  } 
  1. in whichever component your data is changing you have to call this updateSubject method to update data. like this.appService.updateSubject(updatedData);

  2. In ViewAbcComponent component you have subscribe it like

    this.appService.subject.subscribe(x => console.log("mydata",x));

  • Can you please elaborate more – rk01 Mar 02 '21 at 12:55
  • i have edited my comment.. i hope it will clear your doubts? – dinesh sonawane Mar 02 '21 at 13:11
  • I think you did not understand my question. I just want to pass this data: {abc_name: this.abcDetails.item1[keyValue]} when I open dialog box, where abc_name is not just string, it has value. I want that key value on dialog. – rk01 Mar 02 '21 at 13:51