9

In angular 1 I was able to do something like:

<script type="text/ng-template" id="commentView.html">
    <div>
        <div style="float: left; position: absolute;" ng-style="comment.displayPercent">
    </div>
</script>

<script type="text/ng-template" id="commentReplies.html">
    <div>
        <div class="commentChildBoxStyle" ng-hide="comment.hideComment">
            <div style="min-width: 250px;">

                <div ng-repeat="comment in comment.replies" ng-include="'commentReplies.html'"></div>
            </div>

        </div>
    </div>
</script>

I realize I can recursively call a component along with it's template in Angular2, but is there a way to recursively build HTML only? The logic seems like it would fit better with the parent component rather than with a series of child components.

1
  • 1
    Search for ngTemplateOutlet Commented Dec 12, 2016 at 21:54

1 Answer 1

24

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.

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

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.