I am using angular custom elements to build an application in which a MatDialog is part of the custom element. I have a MatMenu in the host application as well. Problem is, when I open the mat-dialog on page load and then open the mat-menu, the mat-dialog is not working afterwards, otherwise if I open the menu first and then the mat-dialog, then the menu doesn't work anymore.
You can find the stackblitz at : https://stackblitz.com/edit/angular-nr58ab-tbu38h
I have added the main.js code of the MatDialog application in the index.html itself. The main.js was generated after building the app with ngx-build-plus in prod mode with output-hashing none and single-bundle true.
The MatDialog app code is like this:
app.module.ts
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { NgModule, Injector } from "@angular/core";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { createCustomElement } from "@angular/elements";
import { MatDialogModule, MatButtonModule } from "@angular/material";
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
MatDialogModule,
BrowserAnimationsModule,
MatButtonModule
],
providers: [],
bootstrap: [AppComponent],
entryComponents: [AppComponent]
})
export class AppModule {
constructor(private injector: Injector) {
const myCustomElement = createCustomElement(AppComponent, {
injector: this.injector
});
customElements.define("app-test-data", myCustomElement);
}
ngDoBootstrap() {}
}
app.component.ts
import { Component, Input, TemplateRef } from "@angular/core";
import { MatDialog } from "@angular/material";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
constructor(private dialog: MatDialog) {}
openModal(temp: TemplateRef<any>) {
this.dialog.open(temp, { width: "100px", height: "100px" });
}
}
and the app.component.html
<button mat-raised-button (click)="openModal(modal)">Open</button>
<ng-template #modal>
<mat-dialog-content>
Hello
</mat-dialog-content>
</ng-template>
This is the app I have built and put inside the index.html of the app in the stackblitz.
I am stuck with this for some time now, I have tried running the dialog.open() inside NgZone.run() also, but no luck there either. Thanks.
If you are on Angular version 8 and below. consider upgrading to angular 10.
We too were facing same issue with angular elements based microfrontends. Upgraded application to angular 10 and issue is resolved magically. :)