我有两个数组:books 和 booksDisplay。在我的模板中,我使用 *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/