【发布时间】:2019-10-27 10:56:14
【问题描述】:
我是 Angular 框架的新手,最近被介绍到属性 *ngFor=""。
据我所知,它采用与 JavaScript for() 循环相同的参数...因此,*ngFor="let item of itemCollection"。但是,与其他指令相比,*ngFor 不需要将其周围的[] 视为代码。
This documentation page 建议hero 属性需要[] 将其值视为代码,并返回"hero" 的值:
<app-hero-detail *ngFor="let hero of heroes" [hero]="hero"></app-hero-detail>
为什么会这样? *ngFor 也被评估,但显然不需要 []... 是 * 前缀表示必须始终将值视为代码还是什么?
【问题讨论】:
-
因为它是一个structural directive,而这些都没有,因为它们有能力改变它们所绑定的节点(重复、渲染成 dom 等),而属性绑定则没有'吨。 TL;DR 这是 Angular 人决定的约定……
标签: angular angular-directive ngfor