Angular2 - recursive html without making a new component?

前端 未结 1 809
没有蜡笔的小新
没有蜡笔的小新 2021-02-06 04:30

In angular 1 I was able to do something like:



        
相关标签:
1条回答
  • 2021-02-06 05:08

    There certainly is a html only solution in Angular:

    <h1>Angular 2 Recursive List</h1>
    <ul>
      <ng-template #recursiveList let-list>
        <li *ngFor="let item of list">
          {{item.title}}
          <ul *ngIf="item.children.length > 0">
            <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: item.children }"></ng-container>
          </ul>
        </li>
      </ng-template>
      <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: list }"></ng-container>
    </ul>
    

    Here's a gist.

    0 讨论(0)
提交回复
热议问题