【问题标题】:Angular Nested categories and *ngFor loopAngular 嵌套类别和 *ngFor 循环
【发布时间】:2020-01-23 10:56:47
【问题描述】:

我有一个未定义数量的数组,可以无限嵌套。我将在我列出产品的页面上的“过滤”部分使用它。但我无法动态弄清楚如何在 html 端创建它。

[
    {
      "name": "Models",
      "items": [
        {
          "name": "Fabric",
          "fieldType": "checkbox",
          "subCategories": []
        },
        {
          "name": "Linen",
          "fieldType": "checkbox",
          "subCategories": [
            {
              "name": "Colored",
              "fieldType": "checkbox",
              "subCategories": []
            },
            {
              "name": "Solid Color",
              "fieldType": "checkbox",
              "subCategories": [
                {
                  "name": "Black",
                  "fieldType": "checkbox",
                  "subCategories": []
                },
                {
                  "name": "White",
                  "fieldType": "checkbox",
                  "subCategories": []
                },
                {
                  "name": "Red",
                  "fieldType": "checkbox",
                  "subCategories": []
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "name": "Other",
      "items": [
        {
          "name": "Radio Buttons",
          "fieldType": "checkbox",
          "subCategories": [
            {
              "name": "Radio 01",
              "fieldType": "radio",
              "subCategories": []
            },
            {
              "name": "Radio 02",
              "fieldType": "radio",
              "subCategories": []
            }
          ]
        }
      ]
    }
  ]

在“HTML”方面,我正在尝试创建这样的代码。

  <div class="filter-item">
    <span class="filter-item-title">Models</span>
    <ul>
      <li>
        <mat-checkbox color="primary">Fabric</mat-checkbox>
      </li>
      <li>
        <mat-checkbox color="primary">Linen</mat-checkbox>
        <ul>
          <li><mat-checkbox color="primary">Colored</mat-checkbox></li>
          <li>
            <mat-checkbox color="primary">Solid Color</mat-checkbox>
            <ul>
              <li><mat-checkbox color="primary">Black</mat-checkbox></li>
              <li><mat-checkbox color="primary">White</mat-checkbox></li>
              <li><mat-checkbox color="primary">Red</mat-checkbox></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
  <div class="filter-item">
    <span class="filter-item-title">Other</span>
    <ul>
      <li>
        <mat-checkbox color="primary">Radio Buttons</mat-checkbox>
        <ul>
          <li>
            <mat-radio-group aria-label="Select an option">
              <mat-radio-button color="primary" value="1">Radio 01</mat-radio-button>
              <mat-radio-button color="primary" value="2">Radio 02</mat-radio-button>
            </mat-radio-group>
          </li>
        </ul>
      </li>
    </ul>
  </div>

这是我的现场示例:https://stackblitz.com/edit/angular-yzdxca 如果您能分享您的想法,我将不胜感激。谢谢。

【问题讨论】:

    标签: javascript angular typescript nested angular8


    【解决方案1】:

    我只是给出这个想法,所以以后再适应它。

    这称为递归组件,它是带条件调用的。

    recursive.component.html

    <div>
      your content goes here
    </div>
    <!-- recursive calling -->
    <app-recursive [data]="data" *ngIf="data"></app-recursive>
    

    通过让组件自己调用,只要其中有数据,就可以让它重复。然后,对它应用一个条件,即如果数据为空,则停止显示它。

    但要小心:这种组件很重,如果不管理数据,可能会使应用程序崩溃。我建议您先考虑您的模型并对其进行改进。

    【讨论】:

    • 我将尝试这个想法。谢谢。
    • 谢谢。我尝试了你说的方法,它完全按照我的意愿工作。也许我会把样品留在这里以防他需要另一个朋友stackblitz.com/edit/angular-recursivecomponent
    • 没问题,很高兴能帮上忙!至于链接,如果我是你,我会用代码自己回答(stackblitz 可能有一天会失败,SOF 建议始终将你的代码 sn-ps 放在答案中)。无论如何,再见!
    • fieldType:我会在 html 中检查它是“复选框”还是“收音机”。完成此操作后,我在此处编辑帖子并添加代码。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2017-09-30
    • 2021-01-12
    • 1970-01-01
    • 2019-12-14
    • 2018-10-14
    • 2018-10-13
    • 2019-04-21
    相关资源
    最近更新 更多