How can I set component's ng-content in js with a new component?
I see that the only view composition mentioned in Angular docs is with routings. But what if I don't want routings in my app?
If you want to add from within the component, jnject elementRef: ElementRef
in your components constructor and use elementRef.nativeElement.appendChild(someElement)
.
From the outside just use a different method to get hold of the DOM element and add HTML to it.
I tried a different approach and succeeded, I used DynamicComponentLoader to load my component to an element selector .
DynamicComponentLoader.loadAsRoot(Component1, '#div1, injector);
this method load Component1 to an element with id=div1. DynamicComponentLoader is very imported service that should be in every ng2 book.