Skip to content

Instantly share code, notes, and snippets.

@Katsaros
Last active May 31, 2022 12:17
Show Gist options
  • Save Katsaros/84341b1dbee32400674de9580d46ec4c to your computer and use it in GitHub Desktop.
Save Katsaros/84341b1dbee32400674de9580d46ec4c to your computer and use it in GitHub Desktop.
Angular 2, solve recursion problem with infinity children in nested objects, by showing them as separate divs
<div class="row">
<ng-template #recursiveList let-yourListName>
<div class="col-12" *ngFor="let item of yourListName; let i=index;">
<div class="mystyle" style="text-align: center; " (click)="openDialog(item)">{{item.title}}</div>
<div class="row" *ngIf="item.children.length > 0">
<ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: item.children }"></ng-container>
</div>
</div>
</ng-template>
<ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: yourListName }"></ng-container>
</div>
@Component({
selector: 'app-page',
templateUrl: './page.component.html',
styleUrls: ['./page.component.css']
})
export class PageComponent implements OnInit {
models = [
{
title: '1',
children: []
},
{
title: '2',
children: [
{
title: '2a',
children: []
},
{
title: '2b',
children: [
{
title: '3a',
children: []
},
{
title: '3b',
children: []
},
{
title: '3c',
children: [
{
title: '4a',
children: []
},
{
title: '4b',
children: []
},
]
},
{
title: '3d',
children: [
{
title: '4a',
children: []
},
]
},
]
},
{
title: '2c',
children: [
{
title: '3a',
children: [
{
title: '3b',
children: []
},
{
title: '3c',
children: []
},
{
title: '3d',
children: []
},
{
title: '3e',
children: [
{
title: '4a',
children: []
},
]
},
]
},
]
},
]
},
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment