angular - *ngFor 应该在模板内部使用,还是应该在外部使用?

标签 angular

我见过 *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/

相关文章:

angular - Kendo UI Tabstrip Onclick 事件

javascript - Angular :输入时模糊输入

javascript - 如果指定为 `@ViewChildren` 的读取参数,ViewContainerRef 会引用哪些组件 View

Angular:每次运行 `ng test` 时,Karma 是否都必须编译我的所有代码?

unit-testing - Typescript:如何禁用spec.ts文件生成?

angular - JhiEventManager 多个订阅

node.js - 在不同项目之间共享node_modules

Angular 2 : Using a component in two diffrent ways: as target of a route and by selector <selector-my-component>

angular - 如何在 Angular 5 的测试主机中访问指令的属性?

css - 可滚动涡轮表中的 PrimeNG 过滤器下拉问题