Angular:在ngFor中获取当前索引 [英] Angular: Get current index in ngFor
本文介绍了Angular:在ngFor中获取当前索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下角度:
<div class="test" *ngFor="let item of data; let i = index">
{{item}}
</div>
<div class="function_div" (click)="test(i);">Test Button </div>
然后在.ts
中
export class test{
test(){
console.log(i);
}
}
我想获取一个具有当前索引号的变量.
I want to get a variable with the current index number.
如何处理才能获得当前索引?
How would I go about so that I can get the current index?
谢谢
推荐答案
根据OP对问题的评论:
因此,我正在尝试获取ngFor的索引,以便执行 在触发另一个功能之前与数据数组进行比较.为了 例如,当我单击一个按钮时,将触发test()函数, 然后它将检查当前索引,并对其进行处理.
So, I am trying to get the index of the ngFor in order to do a comparison to array of data before triggering another function. For example, the test() function will be triggered when I click a button, then it will check the current index, and do something with it.
您应该这样做:
<div class="test" *ngFor="let item of data; let i = index">
// In your case this line should be within ngFor loop
<div class="function_div" (click)="test(i);">Test Button </div>
{{item}}
</div>
注意:从不调用以下函数:
{{ test(i) }}
//这将在每次发生任何更改时执行
循环
{{ test(i) }}
// this will be executed each time anything changes in
loop
这篇关于Angular:在ngFor中获取当前索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文