0

Below data should be logically translated into list items.

So the final output is expected like below,

<ul>
    <li>Unordered Item One</li>
    <li>Unordered Item Two</li>
    <li>Unordered Item three</li>
    <li> 
        <ol>
            <li>Ordered Item A</li>
            <li>Ordered Item B</li>
        </ol>
    </li>   
</ul>

I've created recursion component to render all these items,

In Recursion Component,

<ng-container *ngIf="nested.type==='list-item'">
  <li>{{text}}</li>
</ng-container>

But it doesn't render the child items, Please show me what I'm doing wrong here.

4 Answers 4

2

It doesn' t work because you are not having any input property as nested in the recursion component. IMHO, you don't need as many input properties just each Child object will do.

Have something like:

App componenent

<ng-container *ngIf="nested.type==='unordered-list-item'">
    <li>{{nested.text}}</li>
    <ul *ngIf="nested.children.length > 0">
        <app-recursion *ngFor="let child of nested.children" [child]="child"></app-recursion>
    </ul>
</ng-container>

Recursion Component.

<ng-container *ngIf="child?.type==='unordered-list-item'">
  <li>{{child.text}}</li>
  <ng-container *ngIf="child.children?.length > 0">
    <ul>
            <app-recursion *ngFor="let subChild of child.children" [child]="subChild"></app-recursion>
    </ul>
  </ng-container>
</ng-container>

<ng-container *ngIf="child?.type==='ordered-list-item'">
  <li>{{child.text}}</li>
  <ng-container *ngIf="child.children?.length > 0">
    <ol>
            <app-recursion *ngFor="let subChild of child.children" [child]="subChild"></app-recursion>
    </ol>
  </ng-container>
</ng-container>

See an example here: https://stackblitz.com/edit/angular-qzezhp?file=src%2Fapp%2Frecursion%2Frecursion.component.html

EDIT

With the current logic, list type is being decided by the parent, for example, if the last child in the root has type: "ordered-list-item" and its children are of type unordred-list-item then still ordered lists are generated with something like:

<ng-container *ngIf="child?.type==='ordered-list-item'">
  <li>{{child.text}}</li>
  <ng-container *ngIf="child.children?.length > 0">
    <ol>  <-- even though children are un-ordered
            <app-recursion *ngFor="let subChild of child.children" [child]="subChild"></app-recursion>
    </ol>
  </ng-container>
</ng-container>

Solution:

Have another property in each object which tells about the children type, I have named that property as childrenType and used with a dummy data, you will jhave to write your logic to add this property dynamically.

Once this has been done:

You can have something like this in your Recursion component.

  <li>{{child.text}}</li>
    <ng-container *ngIf="child.children?.length > 0">
        <ul *ngIf="child.childrenType==='unordered-list-item'">
            <app-recursion *ngFor="let subChild of child.children" [child]="subChild"></app-recursion>
        </ul>
    <ol *ngIf="child.childrenType==='ordered-list-item'">
      <app-recursion *ngFor="let subChild of child.children" [child]="subChild"></app-recursion>
    </ol>
    </ng-container>

See a revised example here: https://stackblitz.com/edit/angular-list-component-4kmets?file=src/app/app.component.ts

Sign up to request clarification or add additional context in comments.

1 Comment

It as an issue, if it is unordered-list-item it still shows as orderered-list-item stackblitz.com/edit/…
0

change or remove condition in recursion component: *ngIf="nested.type==='unordered-list-item'"

  <ng-container>
      <li>{{text}}</li>
    </ng-container>

1 Comment

But, ordered-list-item is not rendering
0

The thing is that the type of inner children is ordered (not unordered). So, just change the Recursion Component to :

<ng-container *ngIf="nested.type==='ordered-list-item'">
<li>{{text}}</li>
</ng-container>

2 Comments

But, ordered-list-item is not rendering
I do not understand what you mean. You place a condition that does not exist and therefore the data is not displayed. You must change or cancel the condition
0

Send data into your app-recursion component from nested.children array.

So app.component.html should look like this:

<ng-container *ngIf="nested.type==='unordered-list-item'">
    <li>{{nested.text}}</li>    
    <ul *ngIf="nested.children.length > 0">     
    <app-recursion [children]="nested.children"></app-recursion>
    </ul>
</ng-container>

And then use *ngFor in recursion.component.html:

<ng-container >
  <li *ngFor="let child of children" >
      {{child.text}}
  </li>
</ng-container>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.