使用* ngFor获取下一个迭代值

时间:2018-04-30 20:32:36

标签: angular indexing 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;}

无法使其正常工作

1 个答案:

答案 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数组中的第二项