2

Here is my stackblitz link: DEMO

I am trying to append the 'table' component inside the #test of app component, when the visualization type is 'table'. For which, I am calling the createTable() which creates and appends <app-table> tags into the #test div.

<app-table> tag is present in the DOM but not the content of table.component.html (i.e, table works!) This could be because it is a mere tag and not a compiled component.

How do I get the content of table component displayed inside of app component?

5
  • angular.io/guide/dynamic-component-loader Commented Sep 14, 2018 at 11:59
  • @eko : I tried, it did not seem to work for me. The approach could have gone wrong. Could you please help me out? Commented Sep 14, 2018 at 12:03
  • you can add <angular-table></angular-table> in app component html Commented Sep 14, 2018 at 12:14
  • @Exterminator: I want to append the table ONLY when the visualization type == 'table', at a particular iteration. By adding the tags, the table component will always appear. Commented Sep 14, 2018 at 12:15
  • create a global variable naming type anything you want and add *ngif="type === 'table' " to <angular-table></angular-table> Commented Sep 14, 2018 at 12:18

2 Answers 2

3

Try this

 createTable() {
    this.container.clear(); 
    const factory = this.resolver.resolveComponentFactory(TableComponent);
    this.componentRef = this.container.createComponent(factory);  

  }

Your Forked Example:https://stackblitz.com/edit/dashboard-inchara-3xrjkj

Sign up to request clarification or add additional context in comments.

7 Comments

I want the table to get inserted inside #charts and not inside another #container
you mean inside div?
Yes. Inside <div #charts id="test"> </div>
move ng-template inside that div check the stackblitz
Doing so, appends table component just once. Please check appObject, visualization type == table is twice, which means graph-graph-table-graph-table should appear in the DOM. Inspect my stackblitz output to see, app-table tag being inserted twice as per the condition
|
1

Use ComponentFactoryResolver to add components dynamically. This is further explained in the Angular Docs. Added a full solution in stackblitz: ComponentFactoryResolver. In summary this is what I did:

table creation in app.component.ts:

createTable(container) {
  let componentFactory = this.componentFactoryResolver.resolveComponentFactory(TableComponent);
  let viewContainerRef = this.insTable.viewContainerRef;
  viewContainerRef.clear();
  let componentRef = viewContainerRef.createComponent(componentFactory);
  // let e = document.createElement("app-table");
  // container.appendChild(e);
}

The directive used as and anchor point table.directive.ts:

import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[ins-table]',
})
export class TableDirective {
  constructor(public viewContainerRef: ViewContainerRef) { }
}

And the app.component.html:

<div #charts id="test">
  <ng-template ins-table></ng-template>
</div>

And the app.module.ts with proper declarations to make it all work:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ChartModule } from 'angular-highcharts';

import { AppComponent } from './app.component';

import { HttpClientModule } from '@angular/common/http';
import { TableComponent } from './table/table.component';
import { TableDirective } from './table/table.directive';

@NgModule({
  imports: [BrowserModule, ChartModule, HttpClientModule],
  declarations: [AppComponent, TableComponent, TableDirective],
  bootstrap: [AppComponent],
  providers: [HttpClientModule],
  entryComponents: [
    TableComponent
  ]
})
export class AppModule { }

3 Comments

I tried, it did not seem to work for me. The approach could have gone wrong. Could you please help me out?
@IncharaRaveendra please check my full solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.