question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

md-dialog Before close functionality?

See original GitHub issue

Bug, feature request, or proposal:

Feature?

What is the expected behavior?

When user clicks on backdrop or esc the dialog closes. It would be nice to give the user the option to intercept this close method and prevent default behavior based on some condition.

What is the use-case or motivation for changing an existing behavior?

For example if Dialog contains a form, to be able to disable close or display a message (or whatever function he/she wants to run) and prevent the modal from closing.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
willshowellcommented, Sep 7, 2017

@dfmmalaw this won’t be available until https://github.com/angular/material2/pull/6682 lands. It could look like this:

const dialogRef = dialog.open(MyDialogComponent, {disableClose: true});

const escapeEvents = dialogRef.keydownEvents().filter(e => e.key === 'Escape');

Observable.merge(escapeEvents, dialogRef.backdropClick())
  .map(() => confirm('Are you sure you want to cancel?'))
  .filter(confirmed => !!confirmed)
  .first()
  .subscribe(() => dialog.close());
  

1reaction
willshowellcommented, Aug 9, 2017

beforeClose (#4647) will eventually be implemented to give users the ability to respond immediately instead of waiting for the animation to complete. However, it won’t allow cancelling the close.

So in order to cancel the close (e.g. to show a message when the form needs to be saved or whatever), you would have to pass disableClose to the dialog. Then you would be able to subscribe to backdropClick and escapeKeyEvent streams to show the message or close the dialog on your own.

Those two streams are not yet available and that’s what #3460 is all about (particularly the last ~5-6 comments). However, once https://github.com/angular/material2/issues/6330 is completed, that will be easy work to make them part of the dialog’s public api.

In summary, I doubt they’ll be in the next release, but probably in the following 1-2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mdDialog: catch the onClose event - angularjs - Stack Overflow
So I'm looking for way to call a function BEFORE the pop-up actually closes. Something like scope.$on("$mdDialogBeforeClose", function () { ...
Read more >
Services > $mdDialog - AngularJS Material
.md-dialog-content - class that sets the padding on the content as the spec file ... ok: 'Close' }); $mdDialog .show( alert ) .finally(function()...
Read more >
Closing MdDialog with md-dialog-close on button refreshes ...
I have the exact same functionality on two other dialogs except that those two are not prepopulated with forms.
Read more >
Services > $mdDialog - AngularJS Material - Introduction
.md-dialog-content - class that sets the padding on the content as the spec file ... ok: 'Close' }); $mdDialog .show( alert ) .finally(function()...
Read more >
Angular Material - Dialogs - Tutorialspoint
Angular Material - Dialogs, The md-dialog, an Angular Directive, is a container element ... dialogController); function dialogController ($scope, $mdDialog) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found