JSONデータをページに出力する際に問題があります。 console.logを使用してJSONオブジェクトをコンソールに出力できますが、角度を使用しているページは出力できません。 私はまだこれで新しいです。誰かが私が間違っていた場所を指摘できますか?JSONをページに出力する
エラー:
Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
post.service.ts
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
import {Injectable} from "@angular/core";
import {Post} from './post';
@Injectable()
export class PostService{
constructor(private _http:Http){
}
getPosts(){
return this._http.get('https://jsonplaceholder.typicode.com/posts')
.map(res => res.json());
}
}
app.component.ts
import { Component } from '@angular/core';
import {PostService} from './post.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [PostService]
})
export class AppComponent {
private posts;
constructor(private _postService:PostService){
this.posts =_postService.getPosts();
}
}
app.component.html
<h1>
<ul>
<li *ngFor="let post of posts">
{{post.title}}
</li>
</ul>
</h1>
あなたは
データを取得し、それを表示するようにHTTPリクエストにを購読する必要が
post.ts
export class Post{
id?: number;
body: string;
title: string;
}
ありがとうございました。 – user6680