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

2024-03-22

我必须显示书籍类别的层次结构树,但在渲染的 html 中没有获取任何数据。似乎有问题ngTemplateOutput语境。

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

角度版本: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: [],
            },
          ],
        },
      ]
    },
  ];

Adding ? like

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

防止错误。当以下情况时,该表达式返回 nulllist.childCategories为空,而不是抛出它length不可用于null(安全导航或 Elvis 操作员)

I think

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

应该

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

subCategory代替list

StackBlitz 示例 https://stackblitz.com/edit/angular-ttrmvi

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

实现递归 ngFor 循环时清空 ng-template 的上下文数据 的相关文章

随机推荐