【问题标题】:New to Angular 2, attempting to load data into a tableAngular 2 的新手,尝试将数据加载到表中
【发布时间】:2017-11-10 14:30:59
【问题描述】:

让我先说我对 Angular2 很陌生。所以我试图将列表EMPS 加载到我的表中。这是我的 app.component.ts 文件,其中包含我的大部分代码

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

export class Emp {
  name: string;
  email: string;
  office: string;
}

const EMPS: Emp[] = [
  { name: 'test1', email: 'test1@test.com', office: 'NY' },
  { name: 'test2', email: 'test2@test.com', office: 'LA' },
  { name: 'test3', email: 'test3@test.com', office: 'CHA' }
];

@Component({
  selector: 'my-app',
  template: `
<h1>{{ title }}</h1>
<div *ngFor="let d of data | async">
    <table border=1>
        <tr>
            <td>
                <h3>name: {{ d.name }}</h3>
            </td>
            <td> 
                <h3>email: {{ d.email }}</h3>
            </td>
            <td>
                <h3>office: {{ d.office }}</h3>
            </td>
            <td>
        </tr>
    </table> 
</div>
  `,
  styles: [`

  `]
})
export class AppComponent {
  title = 'TestTable';
  emps = EMPS;
  data = this.emps;

}

这是我得到的错误:

我现在很迷茫,不知道该怎么办。谁能帮帮我?

【问题讨论】:

    标签: javascript angular html-table


    【解决方案1】:

    在您的*ngFor 中放弃使用async 管道。这应该用于迭代 Observable。您使用的是“静态”内容,而不是 Observable。

    【讨论】:

    • 就是这样!
    • 很高兴为您提供帮助!如果你不熟悉 Observables,我会补充一点,但你应该熟悉 RxJS。在 Angular 世界中非常有用和重要。
    猜你喜欢
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多