gpt4 book ai didi

javascript - AgGrid rowData 未在 Observable 对象上的 "subscribe"调用内更新

转载 作者:行者123 更新时间:2023-12-03 01:34:20 26 4
gpt4 key购买 nike

我相当确定这与范围问题有关,但我在网上找不到任何相关信息。我有一个服务从 API 返回一个基本的 Observable,其中包含我想要更新 rowData 变量的数据。

MyService.ts

 fetchDummyData(): Observable<DummyResponse[]> {
return this.http.get<DummyResponse[]>('***SOME_API***');
}

我的组件

如果我尝试在此处向 rowData 添加数据,网格不会更新

rowData = [];
/////////////
////////////Other stuff

this.myService.fetchDummyData().subscribe(
data => {
for (let i = 0; i < data.length; i++) {
this.rowData.push(
{
idType: 'ID Type ' + (i + 1),
description: 'adfasdf'
}
)
//This log prints the correct thing, but rowData remains empty outside the loop
console.log('Row id: ' + this.rowData[i].idType);
}
}
)

但是如果我在上面的订阅调用之后添加它,它就会起作用。

rowData = [];
/////////////
////////////Other stuff

this.myService.fetchDummyData().subscribe(
data => {
for (let i = 0; i < data.length; i++) {
//Do Nothing
}
}
)

//This actually updates the rowData
this.rowData.push(
{
idType: 'ID Type ' + (i + 1),
description: 'adfasdf'
}
)

为什么 rowData 不在订阅调用中全局更新?

最佳答案

您可以在订阅者内部使用api.setRowData(newData)

this.myService.fetchDummyData().subscribe(
data => {
let rows = [];
for (let i = 0; i < data.length; i++) {
rows.push({
idType: 'ID Type ' + (i + 1),
description: 'adfasdf'
});
}
this.gridApi.setRowData(rows);
}
)

关于javascript - AgGrid rowData 未在 Observable 对象上的 "subscribe"调用内更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51143861/

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