使用 *ngFor 获取下一次迭代值
Get Next Iteration Value with *ngFor
我试图在控制器中获取 *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;}
无法正常工作
如果我猜对了,试试这个:
<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
数组中的第二个项目
我试图在控制器中获取 *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;}
无法正常工作
如果我猜对了,试试这个:
<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
数组中的第二个项目