簡體   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