gpt4 book ai didi

数据更改后 Angular 模态数据不刷新

转载 作者:行者123 更新时间:2023-12-03 18:32:08 25 4
gpt4 key购买 nike

我有以模式显示的数据。但是,当我的数据更新并且父级检测到这一点时,我的模态数据永远不会更新。我错过了什么吗?
parent :

  @Input() players: Players[] = [];

showPlayers() {
const modalRef = this.modalService.open(PlayersModalComponent, {
data: { players: this.players }
},
});
}

ngOnChanges(changes: SimpleChanges) {
console.log('changes', changes);
// This shows that players has changed and gone from say 2 players to 3 players after a few actions. But modal doesn't update.
}
模态
this.players = this.modalRef.data.players;
模态 HTML
Here I just list the players and initially they are perfect. But they do not update when a new player is added and ngChanges in parent ALSO shows there is a new row.

<modal class="modal" *ngIf="players">
<div>
<div *ngFor="let player of players">
{{player.name}}
</div>
</div>
</modal>
但是,如果添加了新结果 --- 因为我正在从父级对 API 进行脉冲。检测到更改,但模式中的玩家列表未更新。

最佳答案

您可以使用 ngFor trackBy 函数来检测更改

<modal class="modal" *ngIf="players">
<div>
<div *ngFor="let player of players, trackBy: trackItem">
{{player.name}}
</div>
</div>
</modal>
并添加 trackItem 功能
trackItem(item: any): any {
return item.id;
}
项目应该有唯一的 id

关于数据更改后 Angular 模态数据不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62622856/

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