*ngFor没有将任何数据呈现到前端(html页面)



这是我的组件代码:

import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-home-component2',
templateUrl: './home-component2.component.html',
styleUrls: ['./home-component2.component.css']
})
export class HomeComponent2Component implements OnInit {
constructor() { }
ngOnInit() {
}
posts = [{title:'t1', content:'c1'},
{title:'t2', content:'c2'},
{title:'t3', content:'c3'}];
}

**这是我的HTML代码**

<h1>hello</h1>
<h2 *ngIf="posts.length <= 0">no data addedd</h2>
<ul>
<li *ngFor="let post for posts">
<h1>{{post.title}}</h1>
<p>{{post.content}}</p>
</li>
</ul>

*ngFor指令没有向html页面呈现任何数据,甚至没有显示错误。请帮帮我。

它是let post of posts而不是let post for posts

检查https://angular.io/api/common/NgForOf#description更多信息

最新更新