1

I am dynamically creating component inside md-accordion component as below

<md-accordion [displayMode]="displayMode" [multi]="multi"
                class="md-expansion-demo-width">
    <ng-container #piechartsContainer>

    </ng-container>
</md-accordion>

@ViewChild("piechartsContainer", {read: ViewContainerRef}) pieChartContainer: ViewContainerRef;

let componentFactory = this.componentFactoryResolver.resolveComponentFactory(CreatePieChartElement);
let componentRef = this.pieChartContainer.createComponent(componentFactory);

here is the component I am inserting

@Component({
  selector: 'app-create-pie-element',
  template: 
    <md-expansion-panel>
      <md-expansion-panel-header>{{title}}</md-expansion-panel-header>
    </md-expansion-panel>
  ,
  styles: [],
  encapsulation: ViewEncapsulation.None,
})

It is obvious according to angular that it will look <md-accordion><app-create-pie-element><md-expansion-panel>.... in html structure. How to achieve <md-accordion><md-expansion-panel> this structure,

This answer is helpful when creating components by loops. Please help me. Thanks in advance.

Vinujan.S
  • 1,199
  • 14
  • 27

1 Answers1

0

One possible solution could be having ng-template that will wrap CreatePieChartElement template:

CreatePieChartElement.html

<ng-template #tmpl>
  <md-expansion-panel>
    <md-expansion-panel-header>{{title}}</md-expansion-panel-header>
    <p>Some text</p>
  </md-expansion-panel>
</ng-template>

CreatePieChartElement.ts

@ViewChild('tmpl') template: TemplateRef<any>;

Then in parent component

let componentFactory = this.resolver.resolveComponentFactory(CreatePieChartElement);
let componentRef = componentFactory.create(this.pieChartContainer.injector);
...
this.pieChartContainer.createEmbeddedView(componentRef.instance.template);

Plunker Example

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • This is working but I can't achieve one expansion panel expanded at once property ("multi" attribute in md- accordion). It works in normal scenario – Vinujan.S Aug 07 '17 at 09:29
  • I see. Check my updated plunker https://plnkr.co/edit/8a3BvrRCHrFXlzeau5LT?p=preview – yurzui Aug 07 '17 at 09:56