Angular ngFor变量的角函数
假设我有两张桌子;书和作者。Author表包含对书籍ID的引用 在这里,我想循环浏览作者,然后循环浏览该作者的书籍:Angular ngFor变量的角函数,angular,mean-stack,Angular,Mean Stack,假设我有两张桌子;书和作者。Author表包含对书籍ID的引用 在这里,我想循环浏览作者,然后循环浏览该作者的书籍: <div id="authors" *ngFor="let author of authors"> <h1>{{author.name}}</h1> <div id="books" *ngFor="let book_id of author.books" ng-init="let book = getBook(book_
<div id="authors" *ngFor="let author of authors">
<h1>{{author.name}}</h1>
<div id="books" *ngFor="let book_id of author.books" ng-init="let book = getBook(book_id)">
<h2>{{book.title}}</h2>
.....
</div>
</div>
{{author.name}
{{book.title}}
.....
这里的问题是ng init不在Angular 2+中。我需要能够运行一个函数,以便能够从数据库中获取该书,因为作者只持有对该书的引用
我已经看到了一些使用自定义指令的潜在解决方案
我的问题是,这个场景中的最佳实践是什么?或者这个场景就是问题所在,我应该在早期阶段更好地处理数据?我这样做的原因是,如果我不需要某些类型的书籍,我可以隐藏(使用ngIf)它们,而不需要调用DB来获取书籍(这样效率更高?)
我对指令解决方案有点质疑,因为我很惊讶这并没有内置到Angular中,因为这似乎是一个潜在的常见问题,除非他们希望您使用更好的解决方案。您可以使用类似show in的“ngIf”
{{author.name}
{{book.title}}
.....
但是在这个想法和变换数组之间进行选择
<div id="authors" *ngFor="let author of authors">
<h1>{{author.name}}</h1>
<div id="books" *ngFor="let book_id of author.books">
<ng-container *ngIf="getBook(book_id);let book">
<h2>{{book.title}}</h2>
.....
<ng-container>
</div>
</div>