devSK
devSK

Reputation: 2026

Empty context data of ng-template while implementing recursive ngFor loop

I have to show hierarchical tree of book categories but get no data in the rendered html. It seems there is a problem in ngTemplateOutput context.

Tried setting context with implicit and explicit method. When set explicitly e.g. let-list="list" first level of the categories list was rendered, but child categories still weren't.

Angular vesion: 5.2.0

<div class="categories">
  <div *ngFor="let rootCategory of categories">
    <h1>{{rootCategory.name}}</h1>
    <ul>
      <ng-template #recursiveList let-list>
        <li *ngFor="let subCategory of list">
          <div>{{subCategory.name}}</div>
          <ul *ngIf="subCategory.childCategories.length > 0">
            <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: subCategory.childCategories }"></ng-container>
          </ul>
        </li>
      </ng-template>

      <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: rootCategory.childCategories }"></ng-container>
    </ul>
  </div>
</div>

The output is:

<div _ngcontent-c5="" class="categories">
        <!--bindings={
  "ng-reflect-ng-for-of": "[object Object],[object Object"
}--><div _ngcontent-c5="">
          <h1 _ngcontent-c5=""></h1>
          <ul _ngcontent-c5="">
            <!---->

            <!--bindings={
  "ng-reflect-ng-template-outlet-context": "[object Object]",
  "ng-reflect-ng-template-outlet": "[object Object]"
}-->
              <!--bindings={
  "ng-reflect-ng-for-of": "[object Object],[object Object"
}--><li _ngcontent-c5="">
                <div _ngcontent-c5=""></div>
                <!---->
              </li><li _ngcontent-c5="">
                <div _ngcontent-c5=""></div>
                <!---->
              </li><li _ngcontent-c5="">
                <div _ngcontent-c5=""></div>
                <!---->
              </li><li _ngcontent-c5="">
                <div _ngcontent-c5=""></div>
                <!---->
              </li><li _ngcontent-c5="">
                <div _ngcontent-c5=""></div>
                <!---->
              </li><li _ngcontent-c5="">
                <div _ngcontent-c5=""></div>
                <!---->
              </li><li _ngcontent-c5="">
                <div _ngcontent-c5=""></div>
                <!---->
              </li><li _ngcontent-c5="">
                <div _ngcontent-c5=""></div>
                <!---->
              </li><li _ngcontent-c5="">
                <div _ngcontent-c5=""></div>
                <!---->
              </li><li _ngcontent-c5="">
                <div _ngcontent-c5=""></div>
                <!---->
              </li>

          </ul>
        </div><div _ngcontent-c5="">
          <h1 _ngcontent-c5=""></h1>
          <ul _ngcontent-c5="">
            <!---->

            <!---->
          </ul>
        </div><div _ngcontent-c5="">
          <h1 _ngcontent-c5=""></h1>
          <ul _ngcontent-c5="">
            <!---->

            <!---->
          </ul>
        </div><div _ngcontent-c5="">
          <h1 _ngcontent-c5=""></h1>
          <ul _ngcontent-c5="">
            <!---->

            <!---->
          </ul>
        </div><div _ngcontent-c5="">
          <h1 _ngcontent-c5=""></h1>
          <ul _ngcontent-c5="">
            <!---->

            <!---->
          </ul>
        </div>
      </div>

No categories data is shown. Also, an error from the console on this line:

<ul *ngIf="list.childCategories.length > 0">

The error:

ERROR TypeError: Cannot read property 'length' of undefined
    at Object.eval [as updateDirectives]

Simplified categories array:

categories: Category[] = [
    {
      name: 'Category 1',
      childCategories: [
        {
          name: 'Category 1.1',
          childCategories: [
            {
              name: 'Category 1.1.1',
              childCategories: [],
            },
            {
              name: 'Category 1.1.2',
              childCategories: [],
            },
            {
              name: 'Category 1.1.3',
              childCategories: [],
            },
          ],
        },
      ]
    },
  ];

Upvotes: 2

Views: 3491

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657218

Adding ? like

*ngIf="list.childCategories?.length > 0"

prevents the error. This expression returns null when list.childCategories is null, instead of throwing that length is not available on null (safe-natigation or Elvis operator)

I think

<ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: list.childCategories }"></ng-container>

should be

<ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: subCategory.childCategories }"></ng-container>

subCategory instead of list

StackBlitz example

Upvotes: 1

Related Questions