gpt4 book ai didi

angular - 如何将索引传递给组件 angular 2

转载 作者:行者123 更新时间:2023-12-05 02:17:51 24 4
gpt4 key购买 nike

这不是重复的。我已经完成了我的研究。如果您仍然觉得这不是一个好问题,请在评论中写下答案并删除问题。我想做的是将索引值从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com