Angular Tree Component In 6 Lines Of Code
Ok there are more than 6 lines needed to create and style the tree component but the template, where the recursion happens is just six lines in length.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul class="child"> | |
<li *ngFor="let child of children"> | |
{{child.name}} | |
<my-tree *ngIf="child.children" [children]="child.children"></my-tree> | |
</li> | |
</ul> |
We need our data in format where deepening is done with .children property then we just pass them in [children] @Input.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<my-tree [children]="tree"></my-tree> |
Whole plunker with an example is available here:
… easy peasy lemon squeezy