【发布时间】:2018-05-26 10:51:16
【问题描述】:
在我的第一个 Angular 4 应用程序中,我定义了一个列表组件:
<edm-document *ngFor="let document of documents" class="column is-one-quarter"></edm-document>
Document 是一个接口:
export interface Document {
id?: Number,
name: string,
filePath: string
}
一切都按预期工作,即我得到了我的文档列表。但现在我想访问我的DocumentComponent(edm-document 标签组件)中的文档变量
在我的 DocumentComponent 模板中,如果我尝试这样做,它就不起作用:
<p>{{ document.name }}</p>
我收到此错误:DocumentComponent.html:1 ERROR TypeError: Cannot read property 'name' of undefined.
我需要像这样强制执行文档定义,并将文档指定为输入:
<edm-document *ngFor="let document of documents" [document]="document" class="column is-one-quarter"></edm-document>
现在它可以工作了,但对我来说似乎有点多余,因为我在循环中定义了 let。这是否意味着使用let 定义的变量仅在设置了ngFor 指令的标签中可用?
我错过了什么吗?
谢谢,
尼古拉斯
【问题讨论】:
-
是的,您只能访问 edm-document 标签及其所有子标签中的文档变量。
标签: javascript angular ngfor