Here is the shortest and best feasible solution
1 Create Directive "dynamic-template.directive.ts"
import { Directive, Input, TemplateRef } from '@angular/core';
@Directive({
selector: '[dynamic-template]'
})
export class DynamicTemplateDirective {
@Input() id: number;
constructor(public template: TemplateRef<any>) { }
}
2 Define dynamic template in your component helper-content.component.html
<ng-template [dynamic-template] [id]="1">
This is 1st Template
</ng-template>
<ng-template [dynamic-template] [id]="2">
This is 2nd Template
</ng-template>
<ng-template [dynamic-template] [id]="3">
This is 3rd Template
</ng-template>
3 use dynamic template in your component helper-content.component.ts
import { DynamicTemplateDirective } from '@app/components/dynamic-template.directive';
export class HelperContentComponent implements OnInit {
helpTemplate: TemplateRef<any>;
@ViewChildren(DynamicTemplateDirective) templateRefs: QueryList<DynamicTemplateDirective>;
constructor(private eventService: EventEmitterService) {
}
getTemplate(templateId: number): TemplateRef<any> {
return this.templateRefs.toArray().find(x => x.id == templateId).template;
}
showHelperContent(id: number) {
this.helpTemplate = this.getTemplate(id);
}
}
4 Use helpTemplate variable in helper-content.component.html
<div class="content" >
<ng-container *ngTemplateOutlet="helpTemplate"></ng-container>
</div>
*ngTemplateOutlet="(item.ref)"
If that doesn't work, dolet item of [ { ref: 'div' }, { ref: 'span'} ]
and use*ngTemplateOutlet="item.ref"
Check out this example. #40419098 – Trothplight