2017-09-02 66 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如果答案解决了您的问题,然后选择它,以便您的问题关闭 –