2017-02-14 52 views
1

我想从数组呈现数据到HTML表格。这里是我的模型:如何渲染ng-container Angular 2中数组的数据?

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")]; 

这就是我想要的der数据模板:

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

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

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

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

回答

2

它应该是:

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

现在,你试图通过Section模型迭代,不sectionList这是模型的实例。

+1

是的!谢谢....我没有注意到。我纠结于研究角... – Seva