简体   繁体   English

角度4无法找到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到可迭代对象,例如数组

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

Here is the code below: 这是下面的代码:

component.ts component.ts

by selecting match geting id in routing, then taking this id from URL 通过选择匹配的路由中获取ID,然后从网址中获取此ID

export class MatchComponent implements OnInit {
_postArrayMatch: match[];

 constructor(public router:Router, private matchService: MatchService, 
 private route: ActivatedRoute) {}

 ngOnInit(){
 this.getMatchId();
 }

 getMatchId() :void {
 this.route.params.forEach((params: Params)=> {
  let id = +params['id'];



  this.matchService.getMatch(id).subscribe(
    resultArray => this._postArrayMatch = resultArray,
    error => console.log("Error ::" + error))
  })
 }

component.html component.html

just basic interpolation by doing ngFor loop 通过ngFor循环进行基本插值

 <div *ngFor="let post of _postArrayMatch">
 <p>{{post.team1.name}}</p>
 </div>

service.ts 服务

passing the dynamic id 传递动态ID

 getMatch(id:number): Observable<match[]>{
    return this.http.get<match[]>(`http://localhost:3000/match/${id}`)
    }

interface 接口

   export interface match{
     team1:{
      name:string,
      id:number
     }
     team2:{
      name:string,
      id:number
     }
   }

Thats seems the easiest way i could find on how to get the data from objects. 那就是我找到如何从对象中获取数据的最简单方法。

<div *ngFor="let post of objectKeys(_postArrayMatch.team1)">
  <div>  Team1: {{ _postArrayMatch.team1[post]}}</div>
</div>  

component.ts component.ts

objectKeys = Object.keys;

Try something like this where you create the object in your response 尝试类似这样的操作,在响应中创建对象

component.ts component.ts

export class MatchComponent implements OnInit {
_postArrayMatch: match[];
newArr: Array<Object> = [];

 constructor(public router:Router, private matchService: MatchService, 
 private route: ActivatedRoute) {}

 ngOnInit(){
   this.getMatchId();
 }

 getMatchId() :void {
  this.route.params.forEach((params: Params)=> {
  let id = +params['id'];



  this.matchService.getMatch(id).subscribe(
    resultArray => {
      this._postArrayMatch = resultArray;
      const keys = Object.keys(this._postArrayMatch) // new stuff starts here
      for(let i = 0; i < keys.length; i++) { 
        newArr.push(this._postArrayMatch[keys[i]]);
        newArr[i]['team'] = keys[i];
      }
    },
    error => console.log("Error ::" + error))
  })
 }

And then you can access ALL of your sub objects in your html 然后,您可以访问html中的所有子对象

Component.html Component.html

 <div *ngFor="let post of newArr">
  <p> {{post.team}}: {{post.name}}</p>
 </div>

Currently, with what you have you are hard coding for team 1, and if you want to do that then you shouldn't be using the *ngFor 目前,对于您所拥有的,您正在为团队1进行硬编码,如果您想这样做,那么您不应该使用*ngFor

暂无
暂无

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

相关问题 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 Angular 问题:找不到支持“object”类型的 object“[object Object]”的不同。 NgFor 仅支持绑定到诸如 Arrays 之类的 Iterables - Angular problem: 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 such as Arrays how to solve Angular:*ngFor 循环给我错误:找不到类型为“object”的不同支持对象“[object Object]” - Angular: *ngFor loop giving me error: Cannot find a differ supporting object '[object Object]' of type 'object' Error NgFor 只支持绑定到 Iterables,比如数组。 如何将 JSON 对象转换为数组对象? - Error NgFor only supports binding to Iterables such as Arrays. How to convert JSON object to Array Object? 找不到&#39;对象&#39;类型的不同支持对象&#39;[object Object]&#39; - Cannot find a differ supporting object '[object Object]' of type 'object' Angular 12:找不到支持 object 类型为“object”的“[object Object]” - Angular 12: Cannot find a differ supporting object '[object Object]' of type 'object' 找不到其他支持对象&#39;[对象对象] - Cannot find a differ supporting object '[object Object] 过滤多面阵列,但管道返回错误 - 找不到&#39;对象&#39;类型的不同支持对象&#39;[object Object]&#39; - Filtering a multifaceted array, but the pipe return an error - Cannot find a differ supporting object '[object Object]' of type 'object' 找不到类型为“object”的不同支持对象“[object Object]”。 - 上下文菜单问题 - Cannot find a differ supporting object '[object Object]' of type 'object'. - context menu issues
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM