【发布时间】:2018-07-20 13:56:53
【问题描述】:
我有两个数组:books 和 booksDisplay。在我的模板中,我使用*ngFor 来遍历我的booksDisplay 数组:
<div *ngFor="let bookDisplay of booksDisplay">
<div class="book">
<div class="title" *ngIf="???">{{ ???.title }}
</div>
</div>
但是在我的*ngFor-Element 中,我需要在我的books-Array 中查找,其中id 等于我的booksDisplay-Array 中的当前bookId。那么如何在*ngFor-Element 中使用类似 where 子句的内容呢?
【问题讨论】:
-
您可以在您的组件中创建一个字典/查找属性,其中键是书籍 ID,并在
*ngFor块内引用它。或者您可以将 2 个数组合并为一个数组并对其进行迭代。 -
在我看来,您应该使用 component.ts 中的方法来过滤这些书籍,然后在 UI 中查找这些书籍。让 UI 进行简单的渲染会提高您的性能。
-
能否请您在stackblitz.com/edit/angular-xcgx1h中举例子
-
添加数组的结构。