简体   繁体   English

如何在Angular 7中使用ngFor仅在不提及数组列表中的键的情况下获取对象值

[英]How to get object value only without mentioning key from array list using ngFor in Angular 7

I have an array list which has list of emp objects.我有一个包含 emp 对象列表的数组列表。 I want to iterate in html using *ngFor loop.我想使用 *ngFor 循环在 html 中进行迭代。 But I do not want to give like {{emp.empname}} .但我不想给像{{emp.empname}} I want to get values only with out mentioning the key with emp object.我只想在不提及带有 emp 对象的键的情况下获取值。

const epmloyees = [
                    {'empname': 'kumaresan', 'id': 2}, 
                    {'empname': 'perumal', 'id': 3},
                  ]

HTML code HTML代码

        <tr *ngFor="let emp of epmloyees ">

        <td scope="row" class="font-weight-bold">{{emp}}</td>

You can use keyvalue pipe for Transforms Object or Map into an array of key value pairs..您可以使用keyvalue管道将对象或映射转换为键值对数组。

<div *ngFor="let e of epmloyees">
  <div *ngFor="let p of e | keyvalue:desc">
    {{p.key}}{{p.value}}    //You can get here `key` and `value`
   </div>
</div>


desc = (a, b) => {
   if(a.key < b.key) return b.key;
}

You can try this你可以试试这个

 <tr *ngFor="let emp of Object.values(epmloyees)">
 <td scope="row" class="font-weight-bold">{{emp}}</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM