I've looked and can't quite find what I'm looking for. I have found examples using DynamicComponentLoader, ComponentResolver, and ComponentFactoryResolver, but have not found exactly what I'm looking for.
I'm fine with building a component with a known template (*ngFor="let obj of arr") but what about rendering the page with components stored/injected to the view/variable?
For example:
@Component({
template : '{{html}} or <div [innerHTML]="html"></div>'
})
ngOnInit(){
// simulate getting content from api/database
this.html = `<div>testing <somecomponent></somecomponent></div>`;
// ideally, get content from api/database
// var path = 'some/path';
// myhttpservice.get(path).subscribe((res:any)=>{
// this.html = res.html;
// });
}
I can not get the above code to render "somecomponent". I've tried numerous ways using SafeHtml, ng-content, [innerHTML]="var", etc. No juicy.
Any ideas? I came across 1 post that said it wasn't possible... which I feel like is a challenge.
This type of code works, but again it's a known template inside MyComponent:
const factory = this.componentFactoryResolver.resolveComponentFactory(MyComponent);
const ref = this.viewContainerRef.createComponent(factory);
Maybe I need to override the component template (inside 'factory'), and update the local html variable?
Anyone have any ideas / solutions?