Another more modern approach than using the ngFor directive:
<app-parent>
@for (child of children; track child.id; let idx = $index) {
<app-child [index]="idx" [data]="child" />
}
</app-parent>
Assuming that children
is a collection of data group for each app-child
component you want to render and the app-child
component has input properties to receive this data.
The @for operator was recently added to Angular in version 17. I think this is a way more intuitive approach for repeatedly rendering elements on the page especially for people coming from languages such as Python or C#. More on the @for
block here