html - Angular 6 : How to use an array in template syntax with ngFor?

标签 html arrays angular ngfor

我有两个数组:booksbooksDisplay。在我的模板中,我使用 *ngFor遍历我的 booksDisplay数组:

<div *ngFor="let bookDisplay of booksDisplay">
   <div class="book">
      <div class="title" *ngIf="???">{{ ???.title }}
   </div>
</div>

但在我的 *ngFor 里面-我需要在我的 books 中查找的元素-数组,其中 id等于当前bookId在我的 booksDisplay -大批。那么我怎样才能在我的 *ngFor 中使用类似 where 子句的东西呢? -元素?

最佳答案

像这样的东西应该可以工作。但是,我最好在模型中准备数据,而不是在模板中进行此类计算。

<div *ngFor="let bookDisplay of booksDisplay>
   <div class="book">
      <ng-container *ngIf="checkIfExist(bookDisplay.id)">
        <div class="title">{{ books[bookDisplay.id] }}
      </ng-container>
   </div>
</div>

模板:

checkIfExist(id: number) {
  return this.books.find( book => book['id'] === id ) 
}

关于html - Angular 6 : How to use an array in template syntax with ngFor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51444170/

相关文章:

html - 如何删除内容周围的空间(css)?

javascript - 如何根据地址动态 `generate` 谷歌地图?

python - 如何优化速度以计算 3D 阵列中沿 Z 轴的平均值? Cython 与 Numpy

javascript - 有效地将一个数组中的字段合并到另外两个数组中

javascript - 将 RXJS 可观察结果合并为一个

jquery - 响应式 Bootstrap 3 图像交换翻转

html - 将 ~/Content 中的静态 html 文件包含到 ASP.NET MVC View 中

c - C 中字符串数组和函数的问题

javascript - Angular 2无法在html页面中显示项目列表

javascript - 使用 Angular2 按比例缩放