gpt4 book ai didi

angular - 以 Angular 删除项目后无法刷新表

转载 作者:行者123 更新时间:2023-12-05 08:52:59 24 4
gpt4 key购买 nike

我正在尝试使用 angular 和 spring boot 做一个 CRUD 应用程序。尝试删除项目时,无法刷新表格。这是我的代码:

契约(Contract).component.ts:

<table class="table ">
<thead>
<th>ID</th>
<th> TYPE</th>
<th>MISSION</th>
<th> <img src="assets/plus.png"> </th>
</thead>
<tbody>
<tr *ngFor="let contrat of contrats ">
<td>{{contrat.id}}</td>
<td>{{contrat.type}}</td>
<td>{{contrat.name_mission}}</td>
<td><button class="btn btn-danger" (click)="deleteContrat(contrat)">Delete</button>
<button class="btn btn-primary">Edit</button></td>
</tr>
</tbody>
</table>

契约(Contract).component.ts:

deleteContrat(contrat) {
this.contratService.deleteContrat(contrat.id).subscribe((data) => {
this.contrats = this.contrats.filter(u => u !== contrat);
this.fetchData();
}, (error) => {});
}

fetchData() {
this.contratService.getContrats().subscribe(data => {
this.contrats = data;
});
}

删除服务有效,但无法自动刷新表。

最佳答案

不需要重新加载数据。而是从您的契约(Contract)(我假设它应该是契约(Contract)?)数组中删除已删除的项目,如下所示:

this.contratService.deleteContrat(contrat.id).subscribe( (data)=>{
const deletedContrat = this.contrats.find(x => x.id === contrat.id);
this.contrats.splice(this.contrats.indexOf(deletedContrat), 1);
}

关于angular - 以 Angular 删除项目后无法刷新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54921489/

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