As Eric Martinez pointed out that this is a known bug related to the use of loadAsRoot . The suggested solution is to use loadNextToLocation or loadIntoLocation .
This was problematic for me, since the component that I was trying to dynamically load was a modal dialog from inside the component with fixed css positioning. I also wanted the ability to load a modal from any component and enter it at the same position in the DOM, regardless of which component it was dynamically loaded from.
My solution was to use forwardRef to insert my AppComponent root into a component that wants to dynamically load my modal.
constructor ( ......... ......... private _dcl: DynamicComponentLoader, private _injector: Injector, @Inject(forwardRef(() => AppComponent)) appComponent) { this.appComponent = appComponent; }
In my AppComponent , I have a method that returns an ElementRef application
@Component({ selector: 'app', template: ` <router-outlet></router-outlet> <div #modalContainer></div> `, directives: [RouterOutlet] }) export class AppComponent { constructor(public el:ElementRef) {} getElementRef():ElementRef { return this.el; } }
Return to my other component (the one I want to dynamically load from modal). Now I can call:
this._dcl.loadIntoLocation(ModalComponent, this.appComponent.getElementRef(), 'modalContainer').then(component => { console.log('Component loaded') })
Perhaps this will help others with similar problems.
source share