如何将 ngFor 循环的索引值传递到组件中?

17 浏览
0 Comments

如何将 ngFor 循环的索引值传递到组件中?

这个问题已经在此处有答案

ngFor with index as value in attribute

我在代码中使用了ngFor循环,我想在点击这个div时将索引值传递给typescript文件。

我是Angular 2的新手,任何帮助都将受到赞赏。

例如:

`
`

admin 更改状态以发布 2023年5月22日
0
0 Comments

`
passIndexValue(index){
   console.log(index);//clicked index
}

你还可以像这样将值传递给组件(假设以下使用了 @Input)

    
    
`

然后在组件对象中取出该值:

@Input() index: number;

可以直接在子组件的模板中使用它:


从而使组件能够根据 *ngFor 循环具有唯一的 ID。

0