You could use a component like
beta.17
@Component({
selector: 'dcl-wrapper',
template: '', //`<div #target></div>`
})
export class DclWrapper {
// alternative way of getting a `ViewContainerRef` for an element inside the view instead of the host itself
//@ViewChild('target', {read: ViewContainerRef}) target;
constructor(private dcl:DynamicComponentLoader, private target:ViewContainerRef) {}
@Input() type;
ngOnChanges() {
if(this.cmpRef) {
throw 'currently changing type after the component was added is not supported'
}
this.dcl.loadNextToLocation(this.type, this.target).then((cmpRef) => {
this.cmpRef = cmpRef;
});
}
}
Plunker beta.17
!! <= beta.15 syntax !!
@Component({
selector: 'dcl-wrapper',
template: `<div #target></div>`
})
export class DclWrapper {
constructor(private elRef:ElementRef, private dcl:DynamicComponentLoader) {}
@Input() type;
ngOnChanges() {
if(this.cmpRef) {
this.cmpRef.dispose();
}
this.dcl.loadIntoLocation(this.type, this.elRef, 'target').then((cmpRef) => {
this.cmpRef = cmpRef;
});
}
}
(from Angular 2 dynamic tabs with user-click chosen components)
Then in your component use it like
@Component({
directives: [DclWrapper],
template: `
<dcl-wrapper [type]="types[node.type]"></dcl-wrapper>
`})
export class ParentComponent {
types = {
application: PmApplicationComponent,
proxy: PmProxyComponent,
part: PmPartComponent,
certificate: PmCertificateComponent,
portal: PmPortalComponent,
};
}
You can't use binding like [node]="node"
for components added DynamicComponentLoader but you can pass data to DclWrapper
and pass the data imperatively to the wrapped component. If this approach seems interesting to you I can elaborate a bit more.