我試圖從這個站點獲取JSON格式的數據https://api.chucknorris.io/(隨機笑話),但我得到一個錯誤:錯誤錯誤:無法找到不同的支持對象'[object Object] '類型'對象'。 NgFor僅支持與陣列等Iterables綁定。而 「DebugContext_ {視圖:對象,nodeIndex:11,nodeDef:對象,elDef:對象,elView:對象}」Angular 2 http - 從API獲取數據
這是routing.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class RandomService {
constructor(private http: Http) {
console.log('working');
}
getRandomJokes() {
return this.http.get('https://api.chucknorris.io/jokes/random')
.map(res => res.json());
}
}
這是我想要的組件數據放入
<div *ngFor="let joke of jokes">
<h3>{{joke.value}}</h3>
</div>
`,
providers: [RandomService]
})
export class PocetnaComponent {
jokes: Joke[];
constructor(private jokesService: RandomService){
this.jokesService.getRandomJokes().subscribe(jokes => {this.jokes = jokes});
}
}
interface Joke{
id: number;
value: string;
}
'getRandomJokes()'沒有返回數組等你笑話.. – echonax