How to communicate from angular-material2 dialog to its parent
Asked Answered
A

4

20

I have Parent component that opens an angular-material2 dialog box.

let dialogRef = this.dialog.open(Child, {
            disableClose: true
        });

opened dialog Child component has a button 'Add'. I want to notify the `Parent' component if user click on 'Add' button.

How is this possible?

Akene answered 10/3, 2017 at 11:39 Comment(1)
atleast give a reason for downvoteAkene
A
46

I used EventEmitter to communicate back to parent container

    // dialog component
    ...
    onAdd = new EventEmitter();
    
    onButtonClick() {
      this.onAdd.emit();
    }
    ... 

and parent component

    // parent component
    ...
    let dialogRef = this.dialog.open(Component);
    const sub = dialogRef.componentInstance.onAdd.subscribe(() => {
      // do something
    });
    dialogRef.afterClosed().subscribe(() => {
      // unsubscribe onAdd
    });
    ...

Here is the demo

http://plnkr.co/edit/KbE3uQi2zMNaZlZEEG5Z

Thanks to thomaspink

Akene answered 14/3, 2017 at 12:47 Comment(2)
Worked . ThanksLiddell
Thank you. that's work for dialog and master page communication. Also good example for understanding about emittersDeflect
P
14

Your Answer is correct but this thing is already mentioned in Angular2 Material Documentation in a simple way as you can subscribe to afterClosed method of dialog Reference (dialogRef as in the example) and you will get the selected option's value from subscribe method's argument (In our case result ).

let dialogRef = this.dialog.open(DialogResultExampleDialog);
dialogRef.afterClosed().subscribe(result => {
  this.selectedOption = result;
});

For more details, you can visit this link https://material.angular.io/components/component/dialog and go to Example Tab and try to understand simple example.

Preachment answered 10/5, 2017 at 8:10 Comment(1)
Your answer deserves an upvote, though in my case the accepted answer is right. I need to communicate with the parent module without closing the modal.Cristiano
S
1

If the parent is a service you can communicate via the service.

However you'll run into circular reference issues if they both reference each other. You can instead use the data parameter when you open the dialog to pass the 'parent' to it (I like to do this via an interface).

So in the component when you inject MAT_DIALOG_DATA you can specify it as a certain interface.

  @Inject(MAT_DIALOG_DATA) public data: IMainMenuDialogData

Then define that for whatever you're passing in.

export interface IMainMenuDialogData
{
    mainMenuService: MainMenuService;
}

In the 'parent' component you pass it in via the data property

this.dialogRef.next(this.dialog.open(MainMenuDialogComponent, {
        width: '100vw',
        height: '100%',
        data: <IMainMenuDialogData> { mainMenuService: this }  ....

If you don't like passing in the whole service you can pass in specific observables, or properties or whatever else you need.

Shiri answered 3/7, 2018 at 4:53 Comment(0)
E
1

With "@angular/material": "~7.0.0" componentInstance no longer exist on MatBottomSheetRef.

It has been replace by instance

Parent implementation:

let dialogRef = this.dialog.open(Component);
const sub = dialogRef.instance.onAdd.subscribe(() => {
  // do something
});
Elenore answered 11/12, 2018 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.