ngFor 不适用于数组
ngFor is not working for an array
这是我的代码:
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 正常工作,我的代码中缺少什么?
根据您提供的 JSON,似乎 this.projects 有另一个名为 projects 的对象。你需要
<span *ngFor="let project of projects.projects"> here: {{ project }} </span>
也不需要|json
管道
这是我的代码:
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 正常工作,我的代码中缺少什么?
根据您提供的 JSON,似乎 this.projects 有另一个名为 projects 的对象。你需要
<span *ngFor="let project of projects.projects"> here: {{ project }} </span>
也不需要|json
管道