【问题标题】:Angular recursive template list turns into infinite recursion if object.value is used instead of object如果使用 object.value 而不是 object,则角度递归模板列表将变为无限递归
【发布时间】:2020-10-17 00:16:33
【问题描述】:

我想在 Angular 模板中呈现递归列表。

这是component.ts中的data属性:

data =  [
{
  type: "TEXT",
  key: "text_key_1", 
  value: "text_value_1",
},
{
  type: "INTEGER",
  key: "int_key_1",
  value: 42,
},
{
  type: "ARRAY",
  key: "array_key_1",
  value: [
    {
      type: "TEXT",
      key: "text_key_2", 
      value: "text_value_2",
    },
    {
      type: "TEXT",
      key: "text_key_3", 
      value: "text_value_3",
    },
  ]
}
]

这就是我的模板的样子:

<ul>
  <ng-template #recursiveList let-data>
    <li *ngFor="let item of data">
      <ng-container [ngSwitch]="item.type">
        <ng-container *ngSwitchCase="'TEXT'">{{item.key}}: {{item.value}}</ng-container>
        <ng-container *ngSwitchCase="'INTEGER'">{{item.key}}: {{item.value}}</ng-container>
        <ng-container *ngSwitchCase="'ARRAY'">
          {{item.key}}:
          <ul>
            <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: item.value }"></ng-container>
          </ul>
        </ng-container>
      </ng-container>
    </li>
  </ng-template>
  <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: data }"></ng-container>
</ul>

这是最终结果的样子:

问题是data嵌套在另一个对象mainData中:

mainData =  { data: [
{
  type: "TEXT",
  key: "text_key_1", 
  value: "text_value_1",
},
{
  type: "INTEGER",
  key: "int_key_1",
  value: 42,
},
{
  type: "ARRAY",
  key: "array_key_1",
  value: [
    {
      type: "TEXT",
      key: "text_key_2", 
      value: "text_value_2",
    },
    {
      type: "TEXT",
      key: "text_key_3", 
      value: "text_value_3",
    },
  ]
}
]
}

如果我现在将模板中所有出现的data 更新为mainData.data(例如let-datalet-mainData.data),我会得到一个无限递归。我该如何解决?我认为“。”有问题。我也不确切知道代码是做什么的,因为我从某个地方复制了它。 let-data 和 $implicit 的目的是什么?非常感谢!

【问题讨论】:

    标签: angular typescript angular-components angular-template


    【解决方案1】:

    也许这对你有帮助。

    打字稿

    data: any[] = mainData.data;
    

    HTML

    <ul>
              <ul *ngFor="let item of data">  
                 <ng-container [ngTemplateOutlet]="recursiveListTmpl" [ngTemplateOutletContext]="{ data: item }">
                 </ng-container>
              </ul>
        
        <ng-template #recursiveList let-data="data">
                <li *ngFor="let item of data">
                  <ng-container [ngSwitch]="item.type">
                    <ng-container *ngSwitchCase="'TEXT'">{{item.key}}: {{item.value}}</ng-container>
                    <ng-container *ngSwitchCase="'INTEGER'">{{item.key}}: {{item.value}}</ng-container>
                    <ng-container *ngSwitchCase="'ARRAY'">
                      {{item.key}}:
                      <ul>
                        <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: item.value }"></ng-container>
                      </ul>
                    </ng-container>
                  </ng-container>
                </li>
        </ng-template>
    </ul>
            
        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2020-11-10
      • 2015-05-20
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多