繁体   English   中英

如何在Angular4中删除表中的行。 删除按钮在每一行中

[英]How to delete a row in a table in Angular4 . The delete button is in each row

零件

<tr *ngFor="let item of items; let i = index">
    <th>{{ i +  1 }}</th>
    <th>{{ item.id }}</th>
    <td>{{ item.title }}</td>
    <th><button (click)="deleteItem()">Edit</button></th>
  </tr>

Item.service

deleteItem(){
let header =  new HttpHeaders();
return this._http.delete(this.api);

我假设我应该为该项目添加ID为ID的删除按钮。 但是我不知道如何实现。

我正在使用新的HttpClient

您可以将该item作为参数传递给deleteItem作为

<tr *ngFor="let item of items; let i = index">
  <th>{{ i +  1 }}</th>
  <th>{{ item.id }}</th>
  <td>{{ item.title }}</td>
  <th><button (click)="deleteItem(item)">Edit</button></th>
</tr>

现在,根据您的实际API端点,您可以执行以下操作

deleteItem(item: any){
  return this._http.delete(this.api + '/items/' + item.id);
}

您应该只需要向您的deleteItem()方法添加一个参数即可。

<tr *ngFor="let item of items; let i = index">
    <th>{{ i +  1 }}</th>
    <th>{{ item.id }}</th>
    <td>{{ item.title }}</td>
    <th><button (click)="deleteItem(item.id)">Edit</button></th>
  </tr>

deleteItem(number id){
let header =  new HttpHeaders();
return this._http.delete(this.api);

只需在删除功能中传递商品ID即可

<th><button (click)="deleteItem(item.id)">Edit</button></th>

并在您的delete函数中获取该ID作为参数,然后传递给api

deleteItem(itemId){
    let header =  new HttpHeaders();
    return this._http.delete(this.api);
}

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM