gpt4 book ai didi

angular - AG 网格 : gridReady event not triggered when loading grid in a component via ComponentFactoryResolver

转载 作者:行者123 更新时间:2023-12-04 00:14:54 30 4
gpt4 key购买 nike

在我的 Angular 10 应用程序中,我正在通过组件工厂解析器加载一个包含 AG 网格的组件。
当我通过按钮触发工厂解析器时,一切正常。显示网格和 gridReady事件被正确触发。
但是,如果我通过 ngOnInit() 触发组件工厂解析器或 ngAfterViewInit()等,不触发gridReady事件,不显示网格。
我尝试使用 ChangeDetectorRef 服务来强制更改检测,但这并没有改变任何东西。
这是组件工厂解析器的代码:

export class PlannerComponent implements OnInit {
@ViewChild('ordersContainer', {read: ViewContainerRef}) container: ViewContainerRef;
public orders = [];

constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private httpClient: HttpClient
) { }

ngOnInit(): void {
this.loadOpenOrders();
}

loadOpenOrders() {
this.httpClient.get('/orders')
.pipe(map((result: any) => {
const orders = result._embedded.orders;
orders.forEach((function (order) {
this.addOrder();
}).bind(this))
}))
.subscribe()
}

onGridReady(params) {
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
this.gridApi.sizeColumnsToFit();
}

addOrder() {
// Create component dynamically inside the ng-template
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(OrderComponent);
const newOrder = this.container.createComponent(componentFactory);

// Push the component so that we can keep track of which components are created
this.orders.push(newOrder);
}
}

在 View 中,组件是这样加载的:
<div class="container">
<div class="row">
<div class="col-6">
<div class="card card-custom gutter-b">
<ag-grid-angular
style="width: 100%; height: 600px;"
class="ag-theme-alpine"
[rowData]="ordersToPlan"
[columnDefs]="columnDefs"
[rowSelection]="rowSelectionType"
(gridReady)="onGridReady($event)" <==== Only triggered through button click, but not on ngOnInit()
[animateRows]="true"
>
</ag-grid-angular>
</div>
</div>
<div class="col-6">
<button class="btn btn-success mb-5" (click)="addOrder()">Add a new Order</button>
<div>
<ng-template #ordersContainer>
</ng-template>
</div>
</div>
</div>
</div>

任何帮助表示赞赏:-)

最佳答案

我终于找到了问题所在。我需要强制更改检测,但在子组件中(我通过组件工厂解析器加载的那个)。
order.component.ts :

constructor(
private cd: ChangeDetectorRef
) { }

...

setTransport(transport: Transport): void {
...
this.cd.detectChanges();
}
非常感谢所有试图解决这个问题的人!

关于angular - AG 网格 : gridReady event not triggered when loading grid in a component via ComponentFactoryResolver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64771587/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com