详解Angular2 关于*ngFor 嵌套循环

这篇文章主要介绍了详解Angular2 关于*ngFor 嵌套循环,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

在项目开发中拿到的数据是这样的,要循环遍历出来。可是在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 = [ { 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

   关于ngfor 嵌套循环   {{ item[key] }}

重点的是这个方法

 getKeys(item){ return Object.keys(item); }

结果:

以上就是详解Angular2 关于*ngFor 嵌套循环的详细内容,更多请关注易知道|edz.cc其它相关文章!

推荐阅读