* ng对于ng-template输出没有Angular2

时间:2017-04-03 06:22:25

标签: angular

不确定为什么我的* ngFor循环没有打印出来。我在html文件中有以下代码:

<table class="table table-hover">
    <thead>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Company</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        <!-- NGFOR ATTEMPTED HERE -- no content printed -->
        <ng-template *ngFor="let xb of tempData">
            <tr data-toggle="collapse" data-target="#demo1" class="accordion-toggle">
                <td>{{ xb.name }}</td>
                <td>{{ xb.email }}</td>
                <td>{{ xb.company }}</td>
                <td>{{ xb.status }}</td>
            </tr>
            <!-- other content -->
        </ng-template>
    </tbody>
</table>

然后,在我的简单组件中,我有以下内容:

import { Component }        from '@angular/core';

@Component({
    selector: 'my-profile-exhibitors',
    templateUrl: './profile-exhibitors.component.html',
    styleUrls: ['./profile-exhibitors.component.scss']
})
export class ProfileExhibitorsComponent {
    public tempData: any = [
        {
            'name': 'name1',
            'email': 'email1@gmail',
            'company': 'company',
            'status': 'Complete'
        },
        {
            'name': 'name2',
            'email': 'email2@gmail',
            'company': 'company',
            'status': 'Incomplete'
        }
    ];
    constructor() {}
}

当我运行此代码时,我得到零输出。甚至更奇怪的是,当我使用调试工具选择元素时,我看到了:

enter image description here

看起来它正确识别我的对象,但后来什么也没输出。

2 个答案:

答案 0 :(得分:63)

我认为你想要的是

<ng-container *ngFor="let xb of tempData">

<ng-template ngFor let-xb [ngForOf]="tempData">

答案 1 :(得分:2)

还要获取索引:<ng-template ngFor let-xb [ngForOf]="tempData" let-index="index">