我见过 *ngFor 用于重复父模板中的子模板,并且还看到它在子模板本身内部使用。是否存在应该优先选择其中任何一个的情况?
<div class="results">
<app-result-tile
*ngFor="let d of service.data"
[data]="d">
</app-property-tile>
</div>
或者我应该将 ngFor 移到 <app-result-tile>
中组件?
<div class="results">
<app-result-tiles
[data]="service.data"> <!-- results now repeat internally with li's or something.-->
</app-result-tiles>
</div>
我将编写一个管道来过滤来自父组件的结果组件。在这种情况下使用第一种方法会更有意义吗?
最佳答案
两者都有效。这取决于您的用例。
前一个会创建 app-result-tiles
组件的不同实例以及相关数量的模板,而后一个会创建组件的一个实例和一个模板。
关于angular - *ngFor 应该在模板内部使用,还是应该在外部使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40533460/