I have an error that I don't understand, I have a json and I made an interface when I try to loop on it I get an error in my html.
我有一个错误,我不明白,我有一个json,我做了一个接口,当我试图循环它时,我在我的html中得到了一个错误。
the json format is quite complex for me.
JSON格式对我来说相当复杂。
thanks.
谢谢。
the error in pictures
图片中的错误
json
杰森
{
"images": [
{
"url": "http://24.media.tumblr.com/tumblr_m82woaL5AD1rro1o5o1_1280.jpg",
"id": "MTgwODA3MA"
},
}
interface
接口
export interface Catlist {
images: Image[];
}
export interface Image {
url: string;
id: string;
}
service
服务
getAllCat(): Observable<Catlist> {
return this.http.get<Catlist>("/api/data/cats.json");
}
ts.file
Ts.file
ngOnInit(): void {
this.unsubscribe = this.catService.getAllCat().subscribe((res: Catlist) => {
this.data = res;
})
}
html
HTML
<div class="separate" *ngFor="let cat of data">
<img [src]="cat.url">
</div>
更多回答
Your JSON isn't valid. You can validate JSON with JSONLint. Please post code, logs and error messages as text, not as images. data
is of type CatList
and not iterable. You probably mean data.images
.
您的JSON无效。您可以使用JSONLint验证JSON。请以文本形式发布代码、日志和错误消息,而不是图像。数据属于CatList类型,不可迭代。你可能指的是数据。图像。
优秀答案推荐
As the error message states, you're trying to loop over an object of type Catlist
. You probably meant to iterate over the Catlist.images
array.
正如错误消息所述,您正试图循环访问CatList类型的对象。您的目的可能是迭代Catlist.Images数组。
<div class="separate" *ngFor="let cat of data.images">
<img [src]="cat.url">
</div>
更多回答
我是一名优秀的程序员,十分优秀!