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.

Not able to close modal dialog using dialogRef.close()

See original GitHub issue

I am opening Modal popup in Angular 4 using Material. whereas it is opening properly but I am not able to close it using dialogRef.close(). I am opening AdminComponent as a popup by clicking on button which is present on Home.Component.html.

Admin.Component.ts- `import {MdDialog} from ‘@angular/material’; import {MdDialogRef} from ‘@angular/material’;

export class AdminComponent implements OnInit { constructor( public dialogRef:MdDialogRef <AdminComponent>, private emergencycontacts: EMTServiceService) { } closeModal() { console.log(‘closing’); this.dialogRef.close(); } }`

Admin.Component.html- <td md-dialog-actions> <button md-raised-button (click)="closeModal()" class="btncancel" >Cancel</button></td>

Please help

Issue Analytics

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

github_iconTop GitHub Comments

29reactions
willshowellcommented, Nov 17, 2017

@krisparis can you try this in ErrorDialogContainerComponent?

constructor(
  private errorEventService: ErrorEventService,
  private errorDialog: MatDialog,
  private ngZone: NgZone) {
}

ngOnInit() {
  this.errorEventService.changeEmitted$.subscribe(errorToken => {
    this.ngZone.run(() => {
      if (errorToken && errorToken.error && (!this.errorPopupOpen)) {
        this.openDialog();            
      }
    });
  });        
}
7reactions
willshowellcommented, Nov 17, 2017

@krisparis I think everything on Angular and Angular Material’s end is working as intended, so I wouldn’t await a fix (maybe a docs improvement here at best).

Every use case is different - I think the expected use of the ErrorHandler is to facilitate logging or extra ajax calls or similar, which don’t need to operate within the angular zone. You’ve chosen to bring the error callback into your components (material dialog), so you should do so inside the zone. If you are using errorEventService.changeEmitted$ exclusively for UI updates, I would suggest this:

@Injectable()
export class AppErrorHandler extends ErrorHandler {

  constructor(private errorEventService: ErrorEventService, private ngZone: NgZone){
    super(false);
  }

  handleError(error: any): void {
    // Bring error handling back into the zone so components are aware of error events
    this.ngZone.run(() => {
      this.errorEventService.emitChange({ error });		
    });
  }
}

But also I have only a moderate understanding of how zones work and should be utilized so take it with a grain of salt 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

MatDialog dialog from Angular Material is not closing
You have this error because you open the dialog outside the NgZone. An easy fix is to import the NgZone in the constuctor:....
Read more >
How to Prevent closing of modal Dialog in Angular Dialog ...
In the following sample, the dialog is closed when you enter the username value with minimum 4 characters. Otherwise, it will not be...
Read more >
Angular Material Dialog not closing when I navigate to a new ...
I haven't worked with angular material in a long time, and I'm on mobile. It's possible that component doesn't have the same dialog...
Read more >
mat-dialog-close not working - You.com | The AI Search ...
I tried using a setTimeOut in the dialogRef.afterOpened() to close the dialog from the parent but the behaviour is the same. For one...
Read more >
Routing to Angular Material Dialogs | by John Crowson | ngconf
Many Angular developers chose to use Angular Material because it provides a huge set of ... but it does not return to “/home”...
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