使用动态对象数据在角度模板中显示对象数据

问题描述 投票:0回答:1

我想以角度显示要模板化的对象值。但是我的对象是动态的,所以我不知道它的键。我也尝试了管道键值,但这对我不起作用。我尝试了一个可能的解决方案,但无法完成它。我也将键值作为数组和对象获取,但无法在ng模板中解析这是我尝试过的-

data=[
{'a':12,'b':34,'d':32,'w':3}
{'a':2,'b':4,'d':3,'w':23}
{'a':24,'b':24,'d':13,'w':63}
]
key_name=['a','b','d','w']

在HTML文件中,我正在尝试使用* ngFor

<ion-row class="data-table data-table-row" *ngFor="let data of tableData">
<ion-col> {{data}}</ion-col>
</ion-row>

*****我正在使用离子****但数据正在显示[object][object]我用它写密钥名称时,数据正在显示

{{data.a}}

谢谢

angular ionic-framework ionic2
1个回答
0
投票

您可能必须使用两个*ngFor循环。尝试以下操作

tableData = [
  {'a':12,'b':34,'d':32,'w':3},
  {'a':2,'b':4,'d':3,'w':23},
  {'a':24,'b':24,'d':13,'w':63}
]
<ng-container *ngFor="let data of tableData">    <!-- iterate the `tableData` array -->
  <ng-container *ngFor="let item of data | keyvalue">    <!-- iterate the object in element of the array -->
    {{ item.key }} : {{ item.value }}
  </ng-container>
</ng-container>

或者如果您不想迭代对象的每个属性,则可以使用json管道

json

或者如果您仍然希望使用<ng-container *ngFor="let data of tableData"> <!-- iterate the `tableData` array --> {{ data | json }} </ng-container> 数组访问对象的属性,请尝试以下操作

key_name
© www.soinside.com 2019 - 2024. All rights reserved.