You don't need to use ComponentFactoryResolver
directly if you want dynamic Angular 2 components in ag-grid - you can use ag-grid's provided Angular 2 interface for this.
Let's say you have the following simple Components:
// CubeComponent
import {Component} from '@angular/core';
import {AgRendererComponent} from 'ag-grid-ng2/main';
@Component({
selector: 'cube-cell',
template: `{{valueCubed()}}`
})
export class CubeComponent implements AgRendererComponent {
private params:any;
private cubed:number;
// called on init
agInit(params:any):void {
this.params = params;
this.cubed = this.params.data.value * this.params.data.value * this.params.data.value;
}
public valueCubed():number {
return this.cubed;
}
}
// Square Component
import {Component} from '@angular/core';
import {AgRendererComponent} from 'ag-grid-ng2/main';
@Component({
selector: 'square-cell',
template: `{{valueSquared()}}`
})
export class SquareComponent implements AgRendererComponent {
private params:any;
agInit(params:any):void {
this.params = params;
}
public valueSquared():number {
return this.params.value * this.params.value;
}
}
// from-component.component.html
<div style="width: 200px;">
<button (click)="changeComponentType()">Change Component Type</button>
<ag-grid-ng2 #agGrid style="width: 100%; height: 350px;" class="ag-fresh"
[gridOptions]="gridOptions">
</ag-grid-ng2>
</div>
// from-component.component.ts
import {Component} from '@angular/core';
import {GridOptions} from 'ag-grid/main';
import {SquareComponent} from "./square.component";
import {CubeComponent} from "./cube.component";
@Component({
moduleId: module.id,
selector: 'ag-from-component',
templateUrl: 'from-component.component.html'
})
export class FromComponentComponent {
public gridOptions:GridOptions;
private currentComponentType : any = SquareComponent;
constructor() {
this.gridOptions = <GridOptions>{};
this.gridOptions.rowData = this.createRowData();
this.gridOptions.onGridReady = () => {
this.setColumnDefs();
}
}
public changeComponentType() {
this.currentComponentType = this.currentComponentType === SquareComponent ? CubeComponent : SquareComponent;
this.setColumnDefs();
}
private createRowData() {
let rowData:any[] = [];
for (var i = 0; i < 15; i++) {
rowData.push({
value: i
});
}
return rowData;
}
private setColumnDefs():void {
this.gridOptions.api.setColumnDefs([
{
headerName: "Dynamic Component",
field: "value",
cellRendererFramework: this.currentComponentType,
width: 200
}
])
}
}
// app.module.ts
import {NgModule} from "@angular/core";
import {BrowserModule} from "@angular/platform-browser";
import {RouterModule, Routes} from "@angular/router";
// ag-grid
import {AgGridModule} from "ag-grid-ng2/main";
// application
import {AppComponent} from "./app.component";
// from component
import {FromComponentComponent} from "./from-component.component";
import {SquareComponent} from "./square.component";
import {CubeComponent} from "./cube.component";
const appRoutes:Routes = [
{path: 'from-component', component: FromComponentComponent, data: {title: "Using Dynamic Components"}},
{path: '', redirectTo: 'from-component', pathMatch: 'full'}
];
@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot(appRoutes),
AgGridModule.withComponents(
[
SquareComponent,
CubeComponent,
])
],
declarations: [
AppComponent,
FromComponentComponent,
SquareComponent,
CubeComponent
],
bootstrap: [AppComponent]
})
export class AppModule {
}
Here the button will allow you to dynamically switch between the two components - this could obviously be done at runtime based on some condition you have.
Note too that it might be simpler for you to have one component, and in the actual output do the conditional logic - for example:
// called on init
agInit(params:any):void {
this.params = params;
if(this.params.isCube) {
// cubed
this.value = this.params.data.value * this.params.data.value * this.params.data.value;
} else {
// square
this.value = this.params.data.value * this.params.data.value;
}
}
You can find more information on how to use Angular 2 with ag-Grid here: https://www.ag-grid.com/best-angular-2-data-grid
With that in place, you can refer to https://github.com/ceolter/ag-grid-ng2-example/blob/master/systemjs_aot/app/clickable.component.ts for an example of using a component that supports click events in the grid. For all the examples take a look at https://github.com/ceolter/ag-grid-ng2-example which provides many examples, together with how to package them up with either systemjs, webpack or angular cli