2017-09-02 35 views
1

我正在研究angular 4,並試圖通過單擊十字按鈕來移除標記的功能。所以下面是我的代碼:如何在onclick事件中獲得角度4中的跨度值?

Component.hmtl

<ul id="ul_top_hypers"> 
     <li *ngFor="let hero of technologies"> 
      <span class="badge badge-pill badge-primary" >{{ hero }}<i class="fa fa-cross-circle-right fa-lg" (click)="removeTag1($event)"></i></span> 
     </li> 
</ul> 

Component.ts

removeTag(event: any) { 
    console.log(event.target.parentNode.value); 
    console.log(event.target.value); 
    } 

我想通過點擊十字按鈕,這樣我可以刪除讓每一個li元素的跨度值那個微小的標籤。但我在控制檯中創建了undefind

任何幫助太明顯。

+0

什麼是「跨度值」? –

+0

選中此 https://stackoverflow.com/questions/45757633/how-to-delete-a-particular-row-from-table-in-angular-2-by-clicking-delete-button/45757794#45757794 – SergioEscudero

回答

1

我剛剛加入;let i=index指數變爲可用,
(click)="removeTag1(i)"通過索引到removeTag(...)方法使用索引

<ul id="ul_top_hypers"> 
     <li *ngFor="let hero of technologies;let i=index"> 
      <span class="badge badge-pill badge-primary" >{{ hero }}<i class="fa fa-cross-circle-right fa-lg" (click)="removeTag1(i)"></i></span> 
     </li> 
</ul> 

。如果你傳遞了這個值,你首先需要在數組中找到它的值,然後才能刪除它,如果你傳遞了索引,你就會立即知道要刪除的數組項。

+1

謝謝@Gunter。它是有用的。我是角4的新手。:)再次感謝。 –

+1

@KULDEEPBISEN如果答案解決了您的問題,然後選擇它,以便您的問題關閉 –