最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to get array index in angular 5 html? - Stack Overflow

programmeradmin2浏览0评论

i have one array and i want display array index and it's value also in angular 5 html, here is my array,

Anil K Chaudhary
0: {is_present: "1", name: "Anil K Chaudhary", date: "22"}
1: {is_present: "0", name: "Anil K Chaudhary", date: "20"}

Laxman J chavda
0: {is_present: "0", name: "Laxman J chavda", date: "22"}
1: {is_present: "1", name: "Laxman J chavda", date: "20"}

below is my html code,

<tr *ngFor = "let getAttendanceData_new of getAttendanceData[key]">
  <td>{{getAttendanceData_new}}</td>   // i want to display array Index here. (e.g "Anil K Chaudhary", "Laxman J Chavda")

  <!-- ======== AND ALSO WANT HERE THEIR VALUE ======== -->
  <td *ngFor="let getDays_new of getDays;let i = index">
    <b *ngIf="getAttendanceData.is_present == 1 && getDays_new == getAttendanceData.date" style="color:green">P</b>
    <b *ngIf="getAttendanceData.is_present == 0 && getDays_new == getAttendanceData.date" style="color:lightgrey">A</b>
  </td>

</tr>

i have one array and i want display array index and it's value also in angular 5 html, here is my array,

Anil K Chaudhary
0: {is_present: "1", name: "Anil K Chaudhary", date: "22"}
1: {is_present: "0", name: "Anil K Chaudhary", date: "20"}

Laxman J chavda
0: {is_present: "0", name: "Laxman J chavda", date: "22"}
1: {is_present: "1", name: "Laxman J chavda", date: "20"}

below is my html code,

<tr *ngFor = "let getAttendanceData_new of getAttendanceData[key]">
  <td>{{getAttendanceData_new}}</td>   // i want to display array Index here. (e.g "Anil K Chaudhary", "Laxman J Chavda")

  <!-- ======== AND ALSO WANT HERE THEIR VALUE ======== -->
  <td *ngFor="let getDays_new of getDays;let i = index">
    <b *ngIf="getAttendanceData.is_present == 1 && getDays_new == getAttendanceData.date" style="color:green">P</b>
    <b *ngIf="getAttendanceData.is_present == 0 && getDays_new == getAttendanceData.date" style="color:lightgrey">A</b>
  </td>

</tr>
Share Improve this question asked Jan 23, 2018 at 7:14 Apurv ChaudharyApurv Chaudhary 1,7953 gold badges34 silver badges58 bronze badges 1
  • 1 Please elaborate more so that others can understand and provide the solution that you are expecting. – Pramod Patil Commented Jan 23, 2018 at 7:17
Add a ment  | 

2 Answers 2

Reset to default 3

Till now the best / shortesr answer I found is

Component side :

objectKeys = Object.keys;

Template side :

<div *ngFor='let key of objectKeys(jsonObj)'>
   Key: {{key}}

    <div *ngFor='let obj of jsonObj[key]'>
        {{ obj.title }}
        {{ obj.desc }}
    </div>

</div>

WORKING DEMO

You could use filter of Angular

{{ getAttendanceData_new | filter : {'name':index} }}
发布评论

评论列表(0)

  1. 暂无评论