如何用delete清空一个Observable——Angular



我试图从可观察对象中删除所有内容,但它不起作用:

这是我任职以来所做的:

deleteAllHistory(Histories$:Observable<History[]>){
Histories$.forEach((history)=>{this.deleteHistory(history.id)})
}

当我点击组件中的按钮时触发此函数。这是在我的组件。ts中处理删除的函数:

Histories$=this.facade.Histories$;
histo:History[];
this.Histories$.subscribe(history => {
this.histo = history as History[];

})
onDeleteAll(){
this.historyService.deleteAllHistory(this.histo);
}

但是它没有删除任何东西,我猜是因为我必须从Observable中删除:history $.

感谢您的宝贵时间!

按要求,post调用:

addHistory(history:History){
return this.http.post(this.apiURL, history);
}

这是模板的一部分:

<ng-container *ngIf="Histories$ | async">
<ng-container
*ngFor="let group of Histories$ | async | groupByDay"
>
<strong>{{ group[0] | date:'longDate'}}</strong>
<br />
<br />

<div *ngFor="let history of group[1]">
<mat-list-item  class="historyClicking" (mouseover)="history.showTrash=true" (mouseout)="history.showTrash=false" (click)="onclick()">

<div class="block">

<div matList>{{history.SNS}}</div>
<div matList>{{history.title}}</div>
<div matList>{{history.DMC}}</div>

要让http请求运行,你需要订阅这个可观察对象。下面是我如何使用forkJoin

deleteAllHistory(histories: History[]){
return Observable.forkJoin(
...histories.map(history => this.http.delete(`${this.apiURL}/${history.id}`)
);
}
onDeleteAll(){
this.historyService
.deleteAllHistory(this.histo)
.subscribe(() => console.log('Deleted all'));
}

最新更新