【问题标题】:Angular pass index to pipe管道的角度传递索引
【发布时间】:2018-12-15 03:02:15
【问题描述】:

我正在尝试在我的管道中使用 *ngFor 索引,如下所示:

<td *ngFor="let course of courses | matchesTime:time | matchesWeekday:i ; index as i">{{course.courseName}}</td>

我的管道是这样的:

transform(items: Array<any>, weekday: number): Array<any> {
    return items.filter(
        item => item.weekday === weekday
    );
}

但是工作日结果是不确定的。我相信当管道处理它时索引没有初始化。

编辑: 我添加了一个 stackblitz https://stackblitz.com/edit/angular-5-pipe-index-error

【问题讨论】:

    标签: javascript angular typescript filter pipe


    【解决方案1】:

    试试这个,看看它是否初始化索引

    <td *ngFor="let course of courses | matchesTime:time | matchesWeekday:i | let i=index">{{course.courseName}}</td>
    

    【讨论】:

    • 相同的结果,等于未定义
    • 尝试使用管道,如果这不起作用,我想可能是一个设置索引和课程的包装器 div,然后是它下面的 TD 以及所有管道等。
    • 管道不工作。您是否在考虑这样的事情: {{course.courseName| matchWeekday:i }} 和管道:transform(item: any, weekday: number): Array { if (item.weekday === weekday) { return item.courseName } 返回;因为这在某种程度上等于每个工作日的 0...
    猜你喜欢
    • 2020-09-05
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 2018-06-12
    相关资源
    最近更新 更多