【发布时间】:2018-01-21 06:44:47
【问题描述】:
我有一个带有两个键的多维数组,看起来是这样的。
user: any = {};
// index is from a for loop to add to the user (this part works perfectly)
this.user[index++] = {
Name: name,
Age: age,
}
但是我的问题是我想在我的角度 HTML 模板中使用 *ngFor 循环遍历它,但是我收到一个错误,说它不支持它。所以我的问题是,我怎样才能遍历这个用户数组并打印出数组内每个键的名称和年龄值?
我的尝试:
<li *ngFor="let user of users">{{user.Name}} - {{user.Age}}</li>
我收到的错误:
ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
谢谢。
【问题讨论】:
-
user: any = {};不是数组!。试试user: any = []; -
^ 他没有尝试访问对象键值,只是声明错误。
user: any[] = []应该修复它吧? -
@MohamedAbbas Agh,你是对的。我生命中有一个小时试图解决这个问题。非常感谢!
-
@Nicster15 不客气 :),我将我的评论转换为答案。检查一下。
标签: javascript html arrays angular typescript