0

考虑有这个简单的表:

<nz-table #table [nzData]="users">
    <thead>
    <tr>
        <th>Id</th>
        <th>First Name</th>
        <th>Last Name</th>
    </tr>
    </thead>
    <tbody>
    <tr *ngFor="let item of table.data">
        <td>{{item.id}}</td>
        <td>{{item.firstName}}</td>
        <td>{{item.lastName}}</td>
    </tr>
    </tbody>
</nz-table>

而这个 .ts 文件:

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

interface User {
    firstName: string;
    latName: string;
}

@Component({
    selector: 'app-list',
    templateUrl: './list.component.html',
    styleUrls: ['./list.component.scss']
})
export class ListComponent {
    users: User[] = [];
}

如何在 html 模板中获取firstNamelastName字段的智能感知?我的 IDE 说该item变量具有 typeany并且它应该是 type User

为什么仍然需要模板参考?为什么我们不能只使用<tr *ngFor="let item of users">(除了分页不起作用的事实)?

4

1 回答 1

1

设置nzTemplateModefalse然后就不需要绑定用户nzData喜欢这个了[nzData]="users"。然后就可以直接使用<tr *ngFor="let item of users">

<nz-table [nzTemplateMode]="false" >
    <thead>
        <tr>
            <th>Id</th>
            <th>First Name</th>
            <th>Last Name</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let item of users">
            <td>{{item.id}}</td>
            <td>{{item.firstName}}</td>
            <td>{{item.lastName}}</td>
        </tr>
    </tbody>
</nz-table>
于 2019-12-23T13:14:27.027 回答