【问题标题】:What is the difference between ngFor and *ngForngFor 和 *ngFor 有什么区别
【发布时间】:2016-10-20 16:36:54
【问题描述】:

我无法在任何地方找到对差异的良好描述。与*ngIfngIf 相同

*ngFor 的一个例子

<li *ngFor="let video of page" ...>
  <img src="api/Videos/{{video.id}}/thumbnail">
</li>

还有一个ngFor的例子

<template ngFor let-labelid [ngForOf]="labelLookup | keyValueFilter" >
  <label  [ngStyle]="{'background-color': labelLookup[labelid].color}">
    <input (click)="changeLabel(labelid)" type="radio" name="labelSelector" value="{{ labelid }}" [checked]="labelid == 1">
  </label>
</template>

【问题讨论】:

  • *ngForngFortemplate 的简写,您可以在 angular docs 和 teropa 博客 here 上找到相同的内容

标签: angular typescript


【解决方案1】:

区别在于*ngFor在内部转换为&lt;template ngFor [ngForOf]="..."

它们是等价的,但前者写起来更方便。

显式版本 (&lt;template ngFor ...&gt;) 允许一次将指令应用于多个元素,而隐式版本(简写)仅使用 &lt;template&gt; 标记包装应用指令的元素。

在 Angular 2.0.0 final 中添加了 &lt;ng-container&gt; 元素,允许在行为类似于 &lt;template&gt; 元素(未添加到 DOM)的元素上使用速记语法。

另见

【讨论】:

    猜你喜欢
    • 2017-09-09
    • 2018-01-12
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 2018-04-30
    • 2020-01-23
    • 1970-01-01
    相关资源
    最近更新 更多