Search code examples
angularangular-ngfor

Angular nested `ngFor` using variable from outer loop


I am making a dynamic permissions list, how would I go about using the "item" from:

<div *ngFor="let item of PermissionsList.T1">
  <!-- Some code here -->
</div>

in a inner ngFor loop. I have tried the following, and other similar variations:

<p *ngFor="let item2 of PermissionsList.{{item}}">test</p>

The PermissionsList example is like this:

T1: Array [ "Business", "Recreation", "Vehicles" ]

Recreation: Array [ "Hunting", "Leisure" ]

Hunting: Array [ "Big_Game", "Bird", "Pest" ]

Big_Game: Array [ "Moose", "Elk" ]

​Pest: Array [ "Wild Boar", "Fox" ]

Bird: Array [ "Geese", "Sharp-tailed grouse" ]

​Leisure: Array [ "Hiking", "Bird Watching" ]

Business: Array [ "Trapping", "Utility", "Construction"]

Vehicles: Array [ "Half_ton", "ATV", "SnowMobile" ]


Solution

  • If PermissionsList is an Array of Arrays and you mean to iterate on the second arrays you can try this:

    <div *ngFor="let permission of PermissionsList">
      <div *ngFor="let item of permission ">
        {{ item }}
      </div>
    </div>
    

    If PermissionsList is an Object of Arrays and you just want to display T1 for example, try this:

    <div *ngFor="let t1 of PermissionsList['T1']">
      {{ t1 }}
    </div>