gpt4 book ai didi

javascript - 如何在 ng-container Angular 2 中渲染数组中的数据?

转载 作者:行者123 更新时间:2023-11-30 15:31:12 29 4
gpt4 key购买 nike

我想将数组中的数据呈现到 HTML 表格中。这是我的模型:

export class Section {
public id :number;
public name: string;
constructor(id: number, theName: string) {
this.id = id;
this.name = theName;
}
}

我将其导入并填充到组件中:

import { Section } from "../models/registerSection.model";

数组声明:

sectionList: Array<Section>;

构造函数中填充数组:

    this.sectionList = [new Section(1, "A"),
new Section(2, "B*"),
new Section(3, "C"),
new Section(4, "D")];

这就是我尝试在模板中呈现数据的方式:

        <ng-container *ngFor='let data of Section'>
<tr>
<td colspan="7">{{data.name}}</td>
</tr>
</ng-container>

但是 table 是空的。在 DOM 中,我看到以下内容:

<!--template bindings={
"ng-reflect-ng-for-of": null
}-->

我做错了什么?在调试中,我可以看到该数组包含数据。

最佳答案

应该是:

<ng-container *ngFor='let data of sectionList'>

现在,您正在尝试遍历 Section 模型,而不是作为该模型实例的 sectionList

关于javascript - 如何在 ng-container Angular 2 中渲染数组中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42223092/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com