Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subscribe to angularMaterial Dialog afterClosed?

Tags:

Regarding closing a dialog. https://material.angular.io/components/component/dialog afterClosed is not thenable?

As in official main documentation:

enter image description here

Error

Property 'then' does not exist on type '() => Observable<any>'. [default] Checking finished with 1 errors

I tried subscribe, but doesnt work either.

like image 374
stevek-pro Avatar asked Feb 20 '17 11:02

stevek-pro


People also ask

How do you pass data to dialog of angular materials?

You can pass any kind of data to the component by adding data to the MatDialog's open() options. }, }); You can retrieve the data by injecting MAT_DIALOG_DATA in the opened component.

How do you test for afterClosed?

You can test Angular Material Dialog's afterClosed method this way: import { of } from 'rxjs'; spy on the dialog and return an observable for the afterClosed() method.

Do I need to unsubscribe from afterClosed?

NO you don't need to unsubscribe, as it automatically completes.

What is angular material dialog?

MatDialog creates modal dialogs that implements the ARIA role="dialog" pattern by default. You can change the dialog's role to alertdialog via MatDialogConfig . You should provide an accessible label to this root dialog element by setting the ariaLabel or ariaLabelledBy properties of MatDialogConfig .


2 Answers

No need to unsubscribe from afterClosed() as it auto completes itself: https://github.com/angular/material2/blob/ae41a0ad69ca26c600f0f56c68dd5a1c102d4f1f/src/lib/dialog/dialog-ref.ts#L75

like image 126
mustordont Avatar answered Jan 03 '23 18:01

mustordont


Based on the examples part of their documentation

let dialogRef = this.dialog.open(DialogResultExampleDialog);

dialogRef.afterClosed().subscribe(result => {
  this.selectedOption = result;
});

But you always can convert result to promise if needed

dialogRef.afterClosed().toPromise()

Do not forget to add toPromise support

import "rxjs/add/operator/toPromise";
like image 32
VadimB Avatar answered Jan 03 '23 16:01

VadimB