components: Opening MdDialog in ngOnInit throws ExpressionChangedAfterItHasBeenCheckedError

Bug:

When opening a MdDialog inside ngOnInit of a component a ExpressionChangedAfterItHasBeenCheckedError error is thrown. If the MdDialog is opened in the constructor of a component no error is thrown.

Temp workaround is to open the MdDialog inside a setTimeout(() => this._dialog.open(MyComponent))

What are the steps to reproduce?

http://plnkr.co/edit/aE6LNzRVYnKB01bMej87?p=preview

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

This only seems occur with material-beta.7 and angular 4.2.0+.

Works fine with material-beta.7 and angular 4.1.3.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 20
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I had the same problem here when I tried open the MatDialog:

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'id: undefined'. Current value: 'id: mat-dialog-0'. It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook ?

Another workaround is to put the dialog openning inside a setTimeout callback. e.g.:

  ngOnInit() {
    setTimeout(() => this.dialog.open(MyComponent))
  }

Also, openning the dialog in the construct works in my case.

workaround- use Promise.resolve()

Promise.resolve().then(() => { this.$loadingDialog = this.dialog.open(LoaddingDialogComponent, { width: '350px', disableClose: true }); });

I got the same error today and moving the call into constructor or using setTimeout() fixes it. Why is that?

Why is this issue closed?

Closing as this is actually working as expected as noted by it requiring a feature request in @angular/angular.

If angular/angular#15634 is implemented then this request would also be fulfilled here upon release of the feature.

Actually, isn’t there any way to show a clearer error message?

Landed here from googling around. An easy way to fix this if you want a dialog to open after doing some data check when loading a view is just to put it later in the lifecycle (see lifecycle hooks: https://angular.io/guide/lifecycle-hooks ). Works great for me on the latest one; which fit my needs, AfterViewInit (ngAfterViewInit).

Is there an update on this one yet?

@ThomasBurleson have you seen this?

in my case placing open modal in ngAfterViewInit worked

@julianobrasil yes, as I mentioned in the issue report πŸ˜ƒ