我试图从这个站点获取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