Created
September 17, 2018 18:43
-
-
Save LironHazan/b1ed3cfd20632496b9ee1730a296fc87 to your computer and use it in GitHub Desktop.
my-dialog.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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