【发布时间】:2017-05-25 12:29:45
【问题描述】:
我正在尝试从该站点https://api.chucknorris.io/(随机笑话)获取 JSON 格式的数据,但我收到一个错误:ERROR 错误:找不到类型为“object”的不同支持对象“[object Object]” . NgFor 仅支持绑定到 Iterables,例如 Arrays。以及“DebugContext_{view: Object, nodeIndex: 11, nodeDef: Object, elDef: Object, elView: Object}”
这是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()没有返回数组,所以开你的玩笑..