简体   繁体   English

找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 仅支持绑定到可迭代对象,例如数组。 [Angular 8]

[英]Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.[Angular 8]

I'm trying to Get array data from the following API: http://dradiobeats.x10host.com/api/areas我正在尝试从以下 API 获取数组数据: http : //dradiobeats.x10host.com/api/areas

API应用程序接口

在此处输入图片说明

在此处输入图片说明

My app.component.ts:我的 app.component.ts:

 import { Component, OnInit } from "@angular/core"; import { User } from "./users.model"; import { UsersService } from "./users.service"; @Component({ selector: "app-root", templateUrl: "./app.component.html", styleUrls: ["./app.component.css"] }) export class AppComponent implements OnInit { users$: User[]; constructor(private userService: UsersService) {} ngOnInit() { return this.userService.getUsers().subscribe(Data => (this.users$ = Data)); } }

app.component.html: app.component.html:

 <div *ngFor="let users of users$"> <p>{{ users.name }}</p> </div>

user.service.ts:用户服务.ts:

 import { Injectable } from "@angular/core"; import { HttpClient } from "@angular/common/http"; import { User } from "./users.model"; @Injectable({ providedIn: "root" }) export class UsersService { apiUrl = "http://dradiobeats.x10host.com/api/areas"; constructor(private _http: HttpClient) {} getUsers() { return this._http.get<User[]>(this.apiUrl); } }

user.model.ts:用户模型.ts:

 export class User { name: string; description: string; domain: string; }

Thanks.谢谢。

The API has returned an object containing your data, rather than an array. API 返回了一个包含您的数据的对象,而不是一个数组。 If you console.log() (or look at the link you posted) the result of the API call you will see that it probably has another element inside that is an array eg如果您使用 console.log() (或查看您发布的链接)API 调用的结果,您将看到它内部可能有另一个元素,即数组,例如

{
  data: [... list of items...]
}

So you just need to change the line of code to be:所以你只需要将代码行更改为:

return this.userService.getUsers().subscribe(Data => (this.users$ = Data.data));

users$分配一个空数组,如下所示:

users$: User[] = [];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 7 找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Iterables,比如 Arrays - Angular 7 Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays Angular 9 找不到支持“对象”类型的 object“[对象对象]”的不同。 NgFor 仅支持绑定到诸如 Arrays 之类的 Iterables - Angular 9 Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays 找不到“object”类型的不同支持对象“[object Object]”。 NgFor 仅支持绑定到 Iterables,例如 Arrays。 ? 怎么解决? - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. ? How solve it? 错误找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到可迭代对象,例如数组 - ERROR Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays 找不到支持 object 类型为“object”的“[object Object]”的差异。 NgFor 只支持绑定到 Iterables,比如 Arrays - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables, such as Arrays 错误:找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到Iterables - Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables 找不到支持 object 类型为“object”的“[object Object]”的差异。 NgFor 仅支持绑定到 Iterables,例如 Array。 json 数据 - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Array. json data 错误:“对象”类型的“[对象对象]”。 NgFor 仅支持绑定到 Iterables,例如 Arrays。 - 离子项目 - Error: '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. - Ionic Project 在角度6中找不到类型为“对象”的其他支持对象“ [对象对象]” - Cannot find a differ supporting object '[object Object]' of type 'object' in angular 6 错误:找不到支持 object '[object Object]' 类型 'object' 的差异 - Err: Cannot find a differ supporting object '[object Object]' of type 'object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM