【发布时间】:2018-12-11 03:14:59
【问题描述】:
我相当肯定这与范围问题有关,但我在网上找不到任何关于它的信息。我有一个服务,它从一个 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);
}
}
)
但如果我在上面的 subscribe 调用之后添加它,它会起作用。
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 不在 subscribe 调用中全局更新?
【问题讨论】:
-
欢迎来到Stack Overflow,在 plunk 或 stackblitz 上重现您的问题,以便其他人可以轻松帮助您。还要经历如何创建minimal reproducible example
标签: javascript angular observable ag-grid