Skip to content

Instantly share code, notes, and snippets.

@LironHazan
Created September 17, 2018 18:43
Show Gist options
  • Save LironHazan/b1ed3cfd20632496b9ee1730a296fc87 to your computer and use it in GitHub Desktop.
Save LironHazan/b1ed3cfd20632496b9ee1730a296fc87 to your computer and use it in GitHub Desktop.
my-dialog.component.ts
import {Component, ComponentFactoryResolver, ComponentRef, Inject, OnDestroy, OnInit, ViewChild, ViewContainerRef} from '@angular/core';
import {MAT_DIALOG_DATA} from '@angular/material';
@Component({
selector: 'app-my-dialog',
templateUrl: './my-dialog.component.html',
styleUrls: ['./my-dialog.component.scss']
})
export class MyDialogComponent implements OnInit, OnDestroy {
@ViewChild('target', { read: ViewContainerRef }) viewContainerRef: ViewContainerRef;
componentRef: ComponentRef<any>;
constructor(
private resolver: ComponentFactoryResolver,
@Inject(MAT_DIALOG_DATA) public data: any) { }
ngOnInit() {
const factory = this.resolver.resolveComponentFactory(this.data.component);
this.componentRef = this.viewContainerRef.createComponent(factory);
}
ngOnDestroy() {
if (this.componentRef) {
this.componentRef.destroy();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment