It is not a duplication!
So, basically, this question is very close related to this one: Angular 2 dynamic tabs with user-click chosen components and mostly to it's author also. But with the huge remark - I want to render my component not to the current ElementRef, but to custom element on the body (or simply just to the body.
I have some kind of implementation of Autocomplete directive that should render it's list component to the body like in code below (refactored to use Compiler instead of ComponentResolver after RC.5). This worked fine with RC.4 (partially with RC.5) with some trick of using internal method of ApplicationRef (or using direct DOM manipulations):
@Directive({
selector: '[...]'
})
export class AutoCompleteDirective {
constructor(private appRef:ApplicationRef, private compiler: Compiler, private injector:Injector) {
}
render () {
this.compiler.compileComponentAsync(AutoCompleteTmplComponent)
.then((factory:ComponentFactory<AutoCompleteTmplComponent>) => {
let cmpRef:ComponentRef<AutoCompleteTmplComponent> =
factory.create(this.injector, null, `body`);
// here is the magic coming
(<any>this.appRef)._loadComponent(cmpRef);
// OR (which is more ugly):
// window.document.body.apeendChild(cmpRef.location.nativeElement);
}
}
Finally, the problem - unit test are failing now since TesBed
introduced and we cannot explicitly inject ApplicationRef into tests config. Also the usage of internal method of experimental class, which can be highly possible removed soon, is not looking good and scalable (also as a direct DOM manipulation).
The question - does anyone know more elegant and stable solution?