Created
October 25, 2017 01:23
-
-
Save cgatian/d84b72423d1b40830111ad458358006e to your computer and use it in GitHub Desktop.
This file contains 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
@Component({ | |
selector: 'my-app', | |
template: ` | |
<ng-template #testTemplate let-name> | |
<div>User {{ name }} </div> | |
</ng-template>`, | |
}) | |
export class AppComponent implements OnInit { | |
@ViewChild('testTemplate') testTemplate: TemplateRef<any>; | |
private portalHost: DomPortalHost; | |
constructor( | |
private componentFactoryResolver: ComponentFactoryResolver, | |
private injector: Injector, | |
private appRef: ApplicationRef, | |
private viewContainerRef: ViewContainerRef, | |
) { } | |
ngOnInit() { | |
// Create a portalHost from a DOM element | |
this.portalHost = new DomPortalHost( | |
document.querySelector('#pageHeader'), | |
this.componentFactoryResolver, | |
this.appRef, | |
this.injector | |
); | |
// Create a template portal | |
const templatePortal = new TemplatePortal( | |
this.testTemplate, | |
this.viewContainerRef, | |
{$implicit: 'Bob'}, | |
); | |
// Attach portal to host | |
this.portalHost.attach(templatePortal); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment