实现递归ngFor循环时,ng-template的空上下文数据

时间:2018-02-16 20:13:49

标签: angular angular5 ng-template

我必须显示图书类别的分层树,但在渲染的html中没有数据。好像ngTemplateOutput上下文中存在问题。

尝试使用隐式和显式方法设置上下文。明确设置时,例如let-list="list"呈现了类别列表的第一级,但子类别仍未呈现。

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>

输出结果为:

<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>

未显示任何类别数据。此外,此行的控制台出错:

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

错误:

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

简化类别数组:

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: [],
            },
          ],
        },
      ]
    },
  ];

1 个答案:

答案 0 :(得分:1)

添加?之类的

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

可以防止错误。当list.childCategories为空时,此表达式返回null,而不是抛出length null上无法使用{safe-natigation或Elvis运算符)

我认为

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

应该是

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

subCategory代替list

StackBlitz example