Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Last active August 1, 2023 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetanelBasal/1e81265921551afbf45d36bd98cc7d42 to your computer and use it in GitHub Desktop.
Save NetanelBasal/1e81265921551afbf45d36bd98cc7d42 to your computer and use it in GitHub Desktop.
openDialog<T>(dialogContent: Type<T>) {
const footer = document.createElement('p');
footer.innerText = 'footer';
const dialogContentRef = createComponent(dialogContent, {
environmentInjector: this.injector
})
const dialogRef = createComponent(DialogComponent, {
environmentInjector: this.injector,
hostElement: document.getElementById('dialog-container')!,
projectableNodes: [
// ng-content nodes
[dialogContentRef.location.nativeElement],
// second ng-content (e.g <ng-content select="footer"></ng-content>)
[ footer ]
]
})
this.appRef.attachView(dialogRef.hostView)
}
@GreenFlag31
Copy link

GreenFlag31 commented Jul 31, 2023

Good, but if you have dependencies in your dialogContent, it won't be 'loaded'. For exemple, if you have a Angular Material input, it won't work. It works only in you have simple HTML.

@GreenFlag31
Copy link

Aaaah, found the solution :
You have to attach also the view of the first component you have created to the CD cycle:
this.appRef.attachView(dialogContentRef.hostView).
If you don't do this, the dependancies of the first component won't be correctly working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment