这不是重复的。我已经完成了我的研究。如果您仍然觉得这不是一个好问题,请在评论中写下答案并删除问题。我想做的是将索引值从 HTML 元素传递到组件。
<ul *ngFor="let person of persons; let i= index" [index]="i">
//上面^^^^^ View 的组件
class appComponent{
@Input() index; // this should get the value of index; but it gives undefined
}
最佳答案
不能在同一个组件上使用@Input
,
你可以这样做
<div *ngFor="let person of persons;let i = index">
<div (click)="getIndex(i)">
</div>
<div>`
然后在TS中
getIndex(index){
console.log(index);//clicked index
}
关于angular - 如何将索引传递给组件 angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46793645/