Angular 删除2后更新用户界面
我使用的是HttpClient的delete方法,但之后UI不会更新。我尝试使用Angular 删除2后更新用户界面,angular,Angular,我使用的是HttpClient的delete方法,但之后UI不会更新。我尝试使用markForCheck,但没有效果 public articles: AlArticleShort[] = []; public filteredArticles: AlArticleShort[] = []; public isLoadingInProgress: boolean = true; // in the constructor this.articlesSubscription = thi
markForCheck
,但没有效果
public articles: AlArticleShort[] = [];
public filteredArticles: AlArticleShort[] = [];
public isLoadingInProgress: boolean = true;
// in the constructor
this.articlesSubscription = this.blogDataService.getArticles()
.subscribe(articles => {
this.isLoadingInProgress = false;
this.articles = articles;
this.filteredArticles = articles;
this.articlesSubscription.unsubscribe();
this.changeDetection.markForCheck();
});
public onDeleteArticle(id: string): void {
this.blogDataService.deleteArticle(id).subscribe(() => {
this.articles = this.articles.filter(article => article.id !== id);
// this.changeDetection.markForCheck();
});
}
public deleteArticle(id: string): Observable<void> {
return this.http.delete<void>(`/api/blog/deleteArticle/${id}`);
}
公开文章:AlArticleShort[]=[];
公共筛选器文件:AlArticleShort[]=[];
public isloadingprogress:boolean=true;
//在构造函数中
this.articlesSubscription=this.blogDataService.getArticles()
.订阅(文章=>{
this.isloadingprogress=false;
这个.文章=文章;
this.filteredArticles=物品;
此.articlesSubscribe.unsubscribe();
this.changeDetection.markForCheck();
});
公共onDeleteArticle(id:字符串):无效{
this.blogDataService.deleteArticle(id).subscribe(()=>{
this.articles=this.articles.filter(article=>article.id!==id);
//this.changeDetection.markForCheck();
});
}
公共deleteArticle(id:string):可观察{
返回此.http.delete(`/api/blog/deleteArticle/${id}`);
}
如果我正在记录
articles.lenght
,则删除后的长度是正确的 这里,如果article.id是一个数字,则需要对onDeleteArticle参数id执行类型转换
像
article.id!==编号(id)
因为你检查了类型。因此它可能不会从数组中删除。您缺少响应回调中的
id
参数:subscribe((id)=>{
。这是输入错误吗?@MichaelDid
来自参数到方法。重新分配组件属性通常会导致UI更新。你能给出一个-你的更改检测设置是什么吗?你能显示你的@component decorator代码吗?Michael D,deleteArticle只返回消息,那篇文章成功了完全删除。它不返回id。article.id是字符串。您可以在stackblitz.com中复制此问题。可能很容易找到问题。