angular - 使用承诺删除后如何刷新我的列表
问题描述
在我的 Angular 应用程序中,我想在删除后更新列表,但我不知道如何使用 promise
delete(id: any) {
this.missionService.deleteMission(id);
// .then((res) => {
// this.missionsArray$[id] = null;
// this.missionsArray$ = this.getAllMissions();
// }).catch((error) => {
// console.log('error', error);
// });
}
我尝试使用带注释的代码,它可以工作,但是使用相同的数据使我的列表翻倍(没有删除的项目)
我使用这个函数来构建我的列表:
getAllMissions(): any {
this.missionService.readAllMissions().then(response => {
response.forEach(mission => {
if (mission.data.missionCreator === this._auth.user.id) {
mission.data.id = mission.ref['@ref'].id;
this.missionsArray$.push(mission.data);
} else {
this.missionsArray$ = [];
}
});
});
}
以及删除一项的此功能:
delete(id: any) {
this.missionService.deleteMission(id);
// .then((res) => {
// this.missionsArray$[id] = null;
// this.missionsArray$ = this.getAllMissions();
// }).catch((error) => {
// console.log('error', error);
// });
}
我尝试使用带注释的代码,它可以工作,但是使用相同的数据使我的列表翻倍(没有删除的项目)
你能解释一下如何使用 Promise 进行体面的刷新吗?
解决方案
this.missionsArray$ = []
在重新加载之前清空数组。
delete(id: any) {
this.missionService.deleteMission(id);
.then((res) => {
this.missionsArray$[id] = null;
this.missionsArray$ = []
this.getAllMissions();
}).catch((error) => {
console.log('error', error);
});
}
或者你可以splice
现有的数组
delete(id: any) {
this.missionService.deleteMission(id);
.then((res) => {
this.missionsArray$[id] = null;
for (var i = 0; i < this.missionsArray$.length; i++) {
if (this.missionsArray$[i].id == id) {
this.missionsArray$.splice(i, 1)
}
}
}).catch((error) => {
console.log('error', error);
});
}
推荐阅读
- python - Python - 将数据帧中的值附加到相应的元组元素
- sql - 从 Oracle 中的 XML 列中提取值
- excel - 如何从大型数据集中的列中提取唯一项目列表
- excel - 如何在vba excel中按多个分隔符分割
- angular - 我如何在角度应用程序中使用 xlsx 库删除列
- here-api - HERE 地图 - 如何检测地图中心是否自原始位置移动?
- azure-cognitive-search - 如何在 Azure Search Suggester 中选择分析器(或语言)
- javascript - 如何将 Google App Script 结果返回到外部网页?
- c++ - 在 C++ 中的 SetClipboardData() 之后粘贴不包括记事本的换行符
- wagtail - 禁用 Wagtail “复制”功能