我有两个阵列:书籍和书展。在我的模板中,我使用*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:如何在ngFor的模板语法中使用数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51444170/