As the title says, I'm trying to create a component dynamically, inside a component that was also dynamically created.
I've got this class here
export class DefaultLayoutComponent {
constructor(private cdRef: ChangeDetectorRef, public defaultLayoutService: DefaultLayoutService,
private resolver: ComponentFactoryResolver) {
}
@ViewChild("appAsideContainer", { read: ViewContainerRef }) appAsideContainer: ViewContainerRef;
ngOnInit() {
//other component can call a method on the service, to control the layout...
this.defaultLayoutService.e_openAppAside.subscribe(params => {
let appAsideRef;
//if there are no component inside it already, create one at least
if (this.appAsideContainer.length == 0) {
const appAsideFactory =
this.resolver.resolveComponentFactory(CustomAppAsideComponent);
appAsideRef = this.appAsideContainer.createComponent(appAsideFactory);
}
let appAsideComponent = <CustomAppAsideComponent>appAsideRef.instance;
//create comp. dynamically
const factory = this.resolver.resolveComponentFactory(params.type);
let component = appAsideComponent.container.createComponent(factory);
//append all input values to components
if (params.inputs) {
for (let p in params.inputs) {
component.instance[p] = params.inputs[p];
}
});
}
}
The issue is that members of appAsideComponent aren't accessible. It doesn't seem to be fully instantiated.
CustomAppAsideComponent in question is here
export class CustomAppAsideComponent {
/** custom-app-aside ctor */
constructor() {
}
@ViewChild("container", { read: ViewContainerRef }) public container: ViewContainerRef;
}
And its markup:
<app-aside [fixed]="true" [display]="false">
<ng-template style="border: solid 2px;" #container></ng-template>
</app-aside>
app-aside is a component that generates a sidebar that opens vertically to the right.
I can usually create other components with no problem using this method, but it fails on this one. Both AppAsideComponent and CustomAppAsideComponent are in my module's entryComponent
Anything obvious I'm missing?
