【发布时间】:2019-03-30 00:13:50
【问题描述】:
当 15 个请求结束时,我有一个包含 15 个响应的数组。我想在向数组添加新响应时更新视图而不刷新页面。
loadItem(res: DashboardInfo[]): void {
this.item.push([]); // ERROR
this.item.push([]); // WARNING
this.item.push([]); // INFORMATION
this.item.push([]); // OK
let element;
while ((element = res.pop())) {
let index: number;
if (element.level === "ERROR") index = 0;
else if (element.level === "WARNING") index = 1;
else if (element.level === "INFORMATION") index = 2;
else index = 3;
if (element.path !== undefined) {
if (this.featuresFlag.visibleFeatures.includes(element.path)) {
this.item[index].push(element);
this.item = this.item.slice();
}
}
}
}
【问题讨论】:
-
你想用什么来更新视图?你说你不想刷新 DOM。 DOM 的哪一部分?你如何展示这个嵌套矩阵的东西?
-
@Zlatko 我想在不刷新仪表板的情况下更新仪表板上的卡片。我的意思是,如果我有 4 张卡片并且其中只有 3 张显示在仪表板上,那么当第四个请求结束时,我希望它显示在仪表板上而不刷新它
-
那么它可以通过 trackBy 来实现,正如下面的答案所暗示的那样,或者 OnPush 更改检测策略,或者两者的组合。
标签: javascript arrays angular typescript view