Manel Alonso
Manel Alonso

Reputation: 397

Angular access ngFor index on another ngFor inside array

I have the following object:

itemList = {
    "0": [],
    "1": [],
    "2": [],
    "3": []
};

I also have the following *ngFor:

<div *ngFor="let new of news; let newindex = index;">
  <div *ngFor="let item of itemList.newindex">
  </div>
</div>

As you can see I'm trying to access itemList[0], for example using the newindex created.

Is this possible?

Upvotes: 3

Views: 2390

Answers (2)

Suren Srapyan
Suren Srapyan

Reputation: 68685

Try to use [] notation to access property

<div *ngFor="let new of news; let newindex = index;">
  <div *ngFor="let item of itemList[newindex]">
  </div>
</div>

Your object is more like to array, so consider to make it as array, not an object with numeric property names.

Check Stackblitz

Upvotes: 3

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You need to use the square brackets to get the value of itemList on that parent index newindex

<div *ngFor="let new of news; let newindex = index;">
  <div *ngFor="let item of itemList[newindex]">
  </div>
</div>

Upvotes: 0

Related Questions