0

在 ts 文件中,我有这样的数据:

app.component.ts

 this.images = [{
     asset_id: 'asset_id',
     asset_name: 'asset_name'
 }];

和html模板如下:

app.component.html

test {{images}}
<div *ngFor="let img of images; index as i">
  dddd--<span>{{img.asset_id}}</span>
</div>

结果如下:

在此处输入图像描述

我在这里做错了什么?

4

2 回答 2

1

你有array一个object项目。所以,试试这个:

id: {{images[0]["asset_id"]}}
name: {{images[0]["asset_name"]}}

<div *ngFor="let img of images">
    <span>{{ img.asset_id  }}</span>
</div>

我在Stackblitz上为您创建了一个示例

于 2020-09-19T19:02:21.650 回答
0

test [object Object]是 bcz 您试图在不迭代的情况下打印数组。

要在不迭代的情况下打印对象数组,我们需要使用索引来访问数组值或使用json管道来打印对象或对象数组。

// output: test [ { "asset_id": "asset_id", "asset_name": "asset_name" } ]
test {{ images | json }}

或使用数组索引访问

{{ images[0].asset_id }}
{{ images[0].asset_name }}
...
于 2020-09-19T19:31:39.070 回答