Skip to content

Instantly share code, notes, and snippets.

@cgatian
Created October 25, 2017 01:23
Show Gist options
  • Save cgatian/d84b72423d1b40830111ad458358006e to your computer and use it in GitHub Desktop.
Save cgatian/d84b72423d1b40830111ad458358006e to your computer and use it in GitHub Desktop.
@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