我动态构建一个下拉引导程序组件,如:
<div class="dropdown" >
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">
Selectionner un filtre...<span class="caret"></span>
</button>
<ul class="dropdown-menu" >
<li *ngFor="let v of items" (click) = "onSelect($event);"> <-- ??????
<a class="dropdown-item" href="#">{{ v.val }}</a>
</li>
</ul>
</div>
如何使用onSelect()函数获取所选项目的索引? ($ event是MouseEvent结构)
答案 0 :(得分:3)
在ngFor中声明一个索引:
<li *ngFor="let v of items; let i = index" (click) = "onSelect($event, i);">
<a class="dropdown-item" href="#">{{ v.val }}</a>
</li>