angular - 从角度材料表中动态删除行
问题描述
我无法从模板中删除行。我能够从数据库中删除记录,但不能从模板中删除。
我已经尝试过使用 splice 方法的功能,但我不断收到错误消息。
this.articles.splice 不是函数
delete.component.ts
deleteArticle(id: number) {
return this.httpClient.delete(`${this.AUTH_SERVER}/api/v1/delete-article/${id}`).subscribe((res: any[]) => {
console.log(res);
this.listArticles();
this.snackBar.open('Article deleted!', '', {
duration: 2000
});
});
}
delete.component.html
<button mat-stroked-button color="warn" (click)="this.articleService.deleteArticle(article.id)">
<i class="material-icons">
delete_forever
</i>
Delete
</button>
解决方案
您可以删除/过滤数据样本:
export class AppComponent {
constructor(private http: HttpClient) {}
data: any[] = [];
onFetch() {
this.http
.get("https://dummy.restapiexample.com/api/v1/employees")
.subscribe((res: any) => {
this.data = res.data;
});
}
onDelete(id: any) {
this.http
.delete("https://dummy.restapiexample.com/api/v1/delete/" + id)
.subscribe((res: any) => {
this.data = this.data.filter(x => x.id !== id);
});
}
}
推荐阅读
- sas - 拆分字符串以处理不同的部分并再次组合回一个 var
- excel - 我正在尝试使用可变参数复制范围
- javascript - 在 hyperref 中使用表单中的 TextField 作为 href(或 PushButton)的 URL
- c++ - 在独立的 Xcode 游乐场中运行 c 或 c++ 代码
- django - 带有模板完整路径的 Django 1.11 get_template
- javascript - 更新javascript数组中的嵌套值
- c# - 如何格式化月份中没有前导零的日期?
- php - 从父表 Eloquent 返回包含所有子表的数组
- permalinks - Hugo 中的部分层次结构
- php - 如何通过 Web 套接字将 Dart Map 对象发送到 PHP 服务器?