简体   繁体   English

Angular *ngFor - 无法让我的 Arrays 循环

[英]Angular *ngFor - Cant get my Arrays to loop

This keeps kicking upError: Cannot find a differ supporting object '[object Object]' of type 'object'.这不断出现错误:找不到支持“object”类型的 object '[object Object]' 的不同。 NgFor only supports binding to Iterables such as Arrays. NgFor 仅支持绑定到 Iterables,例如 Arrays。 But it works when I try print it to the console, any help be appreciated big time guys但是当我尝试将它打印到控制台时它会起作用,任何帮助都将不胜感激

Thank you (:谢谢 (:

//TypeScript File :
public names = ["Jimmy" , "Lilly"];
  public ages = [20,25];
  public profile = {
    Name : this.names,
    Age : this.ages
  }

//HTML File :
<div *ngFor = "let i of profile">
    {{i.Name}}
    {{i.Age}}
</div>

The error already mentions what's the issue.该错误已经提到了问题所在。

NgFor only supports binding to Iterables such as Arrays NgFor 仅支持绑定到诸如 Arrays 之类的 Iterables

You need to use an array in order that *ngFor works您需要使用数组才能*ngFor工作

//TypeScript File
public jimmy = { Name: "Jimmy", Age: 20 };
public lilly = { Name: "Lilly", Age: 25 };
public profiles = [jimmy, lilly]
//HTML File :
<div *ngFor = "let i of profiles">
    {{i.Name}}
    {{i.Age}}
</div>

I don't know if I'm sure about what you're trying to accomplish, but binding i.Name is incorrect since you defined Name as an array.我不知道我是否确定您要完成什么,但是绑定 i.Name 是不正确的,因为您将 Name 定义为一个数组。 You could loop over profile.Name using ngFor and then bind the property "name".您可以使用 ngFor 遍历 profile.Name,然后绑定属性“name”。 Changing variable names could be less confusing:)更改变量名可能不会那么混乱:)

<div *ngFor="let i of profile.Name">
{{i.name}}
</div>

Same is applicable for Age同样适用于年龄

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM