我试图让控制器获得* ngFor循环中下一次迭代的值:
模板:
<p *ngFor="let history of histories; let i=index; "
[ngClass]="{'show':history.date==dateseeing}">
<span class="history-date">{{ history.date }}</span>
<span class="history-txt">{{ history.txt }}</span>
<mat-icon
*ngIf="history.date==dateseeing"
(click)="readNext(history[i+1])">keyboard_arrow_down
</mat-icon></p>
控制器:
public readNext(history) {
console.log(history);
const date = history.date;
this.dateseeing = date;}
无法使其正常工作
答案 0 :(得分:0)
如果我找对你,试试这个:
<p *ngFor="let history of histories; let i=index; "
[ngClass]="{'show':history.date==dateseeing}">
<span class="history-date">{{ history.date }}</span>
<span class="history-txt">{{ history.txt }}</span>
<mat-icon
*ngIf="history.date==dateseeing"
(click)="readNext(histories[i+1])">keyboard_arrow_down
</mat-icon></p>
您无法history[i]
,因为历史记录是数组中的项目,并且您想要访问histories
数组中的第二项