Search code examples
angularviewchild

Angular 2 - Beta 16 @ViewChild, loadNextToLocation trying to load a component inside another one dynamically?


I am trying to load component inside another one dynamically it was working before but when update to beta 16 it stopped. I read the change log and changed my code but still no success.

I logged to console what I get from @ViewChild I suppose to get a ViewContainerRef but I get ElementRef which causes the error. How to solve this

please see the code and screen shot of my console log enter image description here

export class ChildWindowComponent implements OnInit{
public contentType: Type;
public childProps: ChildWindowVm;


@Output() okActionEvent = new EventEmitter<any>();
@Output() cancelActionEvent = new EventEmitter<any>();

@ViewChild('childContentPlace') childContentPlace: ViewContainerRef;
constructor(private dcl: DynamicComponentLoader) {
    console.log(this.childContentPlace);
}

ngAfterViewInit() {
    console.log(this.childContentPlace);

    this.dcl.loadNextToLocation(this.contentType, this.childContentPlace).then((tempComp) => {
        tempComp.instance.childVm = this.childProps.childContent;
    });
}

ngOnInit() {

}

okClicked(e:any) {
    this.okActionEvent.emit(null);

}
cancelClicked(e:any) {
    this.cancelActionEvent.emit(null);
}

}

the HTML Code

<div>
<div class="child-window-block" >

</div>
<div class="child-window child-window-size" [style.width.px]="childProps.width" [style.height.px]="childProps.height" [style.top.px]="childProps.top" [style.left.px]="childProps.left">
    <div class="display-flex flex-direction-col fill-parent">
        <div [style.height.px]="childProps.titleHeight">
            <div class="child-window-header display-flex flex-item-center">
                <div class="flex-grow-1">
                    <h3>
                        {{childProps.title}}
                    </h3>
                </div>
            </div>
        </div>
        <div class="div-sep"></div>
        <div class="flex-grow-1 fill-parent" style="overflow-y: auto; overflow-x: hidden;">

                <div class="child-window-content flex-grow-1"  style="height: calc(100% - 42px);">
                    <div #childContentPlace></div>
                    <div>
                        <div class="text-right">
                            <input type="submit" value="Ok" class="child-window-btn" (click)="okClicked()" />
                            <input type="button" value="Cancel" class="child-window-btn" (click)="cancelClicked()" />
                        </div>
                    </div>
                </div>
        </div>

    </div>

</div>
<div class="child-window-back child-window-size" [style.width.px]="childProps.width" [style.height.px]="childProps.height" [style.top.px]="childProps.top" [style.left.px]="childProps.left">

</div>


Solution

  • You need to tell @ViewChild() what to return

    @ViewChild('childContentPlace', 
        {read: ViewContainerRef}) childContentPlace: ViewContainerRef;