这是我的代码:
import { Component } from '@angular/core';
import { ProjectService } from '../../services/project';
import { Project } from '../../models/project';
@Component({
selector: 'projects-component',
template: `
<pre>{{ projects | json }}</pre>
<span>I WORK</span>
<span *ngFor="let project of projects"> here: {{ project | json }} </span>
`,
styleUrls: ['./style.css']
})
export class ProjectsComponent {
projects: Project[];
constructor(
private projectService: ProjectService
) {
this.projectService.projects().then((projects) => {
this.projects = projects;
console.log('Projects:',this.projects);
});
}
}
打印以下内容:
<span *ngFor="let project of projects"> here: {{ project | json }} </span>
应该打印每个项目,但事实并非如此,我首先怀疑可能会有一些css应用于<span>
标记,所以我添加了<span>I WORK</span>
来制作确定没有任何可疑事件发生,也没有。
我的代码中缺少什么才能让* ngFor工作?
答案 0 :(得分:1)
根据你给出的JSON,似乎this.projects有另一个名为projects的对象。你需要
<span *ngFor="let project of projects.projects"> here: {{ project }} </span>
也不需要 |json
管道