详解Angular2 关于*ngFor 嵌套循环
程序员文章站
2022-04-09 12:54:48
在项目开发中拿到的数据是这样的,要循环遍历出来。可是在ng2中好像不能直接遍历object
datas: any = [
{ num: 1, date:...
在项目开发中拿到的数据是这样的,要循环遍历出来。可是在ng2中好像不能直接遍历object
datas: any = [ { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24"}, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24"}, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24"}, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24"}, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24"}, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24"}, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24"}, ];
在搜索之后发现了这种方法可以实现。
array-ngfor.ts
import { component } from '@angular/core'; @component({ selector: 'page-array-ngfor', templateurl: 'array-ngfor.html', }) export class arrayngfor { constructor() { } datas: array<any> = [ { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24" }, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24" }, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24" }, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24" }, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24" }, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24" }, { num: 1, date: "2017-04-12", sellnum: "1231234", sellmpney: "1233", returnnum: "24" }, ]; getkeys(item) { return object.keys(item); } }
array-ngfor.html
<ion-header> <ion-navbar> <ion-title>关于ngfor 嵌套循环</ion-title> </ion-navbar> </ion-header> <ion-content> <ion-row *ngfor="let item of datas"> <ion-col *ngfor="let key of getkeys(item)"> {{ item[key] }}</ion-col> </ion-row> </ion-content>
重点的是这个方法
getkeys(item){ return object.keys(item); }
结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。