如果您想在Ag-Grid中使用動態的Angular 2組件,則不需要直接使用ComponentFactoryResolver
- 您可以使用ag-grid提供的Angular 2接口。
比方說,你有以下幾個簡單的組件:
// 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 {
}
這裏的按鈕,可以動態地在兩個組件之間切換 - 這可能明顯在運行時根據你有一定的條件下進行。
還要注意,這可能是簡單的讓你有一個組件,並在實際輸出做條件邏輯 - 例如:
// 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;
}
}
你可以找到關於如何使用角2的更多信息ag-Grid here:https://www.ag-grid.com/best-angular-2-data-grid
有了這個功能,您可以參考https://github.com/ceolter/ag-grid-ng2-example/blob/master/systemjs_aot/app/clickable.component.ts瞭解使用支持網格中的點擊事件的組件的示例。對於所有的例子在https://github.com/ceolter/ag-grid-ng2-example它提供了許多例子,一起看看如何將它們打包到了要麼systemjs,的WebPack或角CLI
這樣,它只是不工作。如果你想動態添加Angular上下文,你必須使用'DynamicComponentResolver'來進一步移動。 – micronyks
你可以舉一些如何使用它的例子嗎? –
在StackOverflow中有很多示例可用。 – micronyks