我有一个循环,可将数据推送到数组中,因此我可以用firebase数据填充卡。我试图在单击按钮时简单地返回相对卡的pushId。我可以显示所需的信息,但正在访问函数中的相对pushId。
export class EventListPage {
public eventList: Array < any > ;
keys: any[];
constructor(
public navCtrl: NavController,
public eventProvider: EventProvider
) {}
ionViewDidLoad() {
this.eventProvider.getEventList().on("value", eventListSnapshot => {
this.eventList = [];
eventListSnapshot.forEach(snap => {
this.eventList.push({
id: snap.key,
name: snap.val().name
});
}
upvote() {
//problem area - looking to return the "id" of the clicked button. I dont know how many records there will be.
console.log(this.eventList.id); //obviously doesnt work
console.log(this.eventList[1].id); //returns an id but its not relative to the button clicked obviously..
{
goToEventDetail(eventId: string): void {
this.navCtrl.push('EventDetailPage', {
eventId: eventId
});
}
}
HTML:
<ion-content padding>
<ion-list>
<ion-item *ngFor="let event of eventList">
<h2>{{event?.name}}</h2>
<p>id: <strong>{{event.id}}</strong></p>
<button shape="round" color="primary" expand="block" (click)="upvote()">Upvote</button>
</ion-item>
</ion-list>
</ion-content>
将upvote()函数移至ionViewDidLoad()函数并将其作为嵌套函数访问(我不知道如何做),或者尝试从单击的卡中推入id是一种更好的方法吗?变成变量,并在不嵌套的情况下在upvote函数中使用它?
任何帮助将不胜感激。
提前谢谢!