3

I am trying to dynamically create the component to register in Golden Layout. Eg,

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

ngAfterViewInit(){
    this.myLayout.registerComponent('testComponent', (container, componentState) => {
        //Create an empty div on the container

        container.getElement().html("<div #placeholder></div>");
        this.componentFactory = this.componentFactoryResolver.resolveComponentFactory(TestComponent);
        this.cmpRef = this.viewContainerRef.createComponent(this.componentFactory);
        this.cmpRef.changeDetectorRef.detectChanges();
        GlDirective.componentCount++;
    });
    }

However , because viewContainerRef is referring to ViewChild that only gets created now , it is always undefined. How can we create a component in RC5 that uses dynamically added div like the one above. I used @Günter Zöchbauer 's answer on Angular 2 dynamic tabs with user-click chosen components to derive to this. However unsure on how this can be achieved with Golden Layout which needs the DOM to be generated on the fly. Please help.

B--rian
  • 5,578
  • 10
  • 38
  • 89
Rajee
  • 272
  • 2
  • 8

1 Answers1

1

This is not supported.

Something like #placeholder in

container.getElement().html("<div #placeholder></div>");

will just be added to the browser and not recognized or processed in any way by Angular2 (except sanitization for security purposes).

A template variable can only be created by adding it statically to a components template.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • As per http://plnkr.co/edit/tUQOev?p=preview we could load added div using DCL which is deprecated now . Any idea how can this be acheived ? – Rajee Aug 19 '16 at 15:43
  • I know that DCL supported CSS selectors and no, I don't know if there is a way to do this with currently available APIs :-/ – Günter Zöchbauer Aug 19 '16 at 15:46
  • The only way i can make this work is to use DynamicComponentLoader , however not sure if that's the correct way – Rajee Aug 19 '16 at 16:23
  • `DynamicComponentLoader` is deprecated since quite some while and AFAIK already removed entirely from the source code. – Günter Zöchbauer Aug 19 '16 at 18:42