【问题标题】:Recursive component loading with recursive array使用递归数组加载递归组件
【发布时间】:2016-12-13 18:32:10
【问题描述】:

我正在尝试使用递归数组递归加载 Angular 2 组件(plunk:http://plnkr.co/edit/3npsad?p=preview)。

给定这个递归数组:

[
  {
    "id": 1,
    "text": "abc"
  },
  {
    "id": 2,
    "text": "def",
    items: [
      {
        "id": 21,
        "text": "def-1",
        items: [
          {
            "id": 211,
            "text": "def-1-1"
          },
          {
            "id": 212,
            "text": "def-1-2"
          }
        ]
      },
      {
        "id": 22,
        "text": "def-2"
      }
    ]
  }
]

我需要以下输出:

<my-item>
    <div id="1" text="abc"></div>
</my-item>
<my-item>
    <div id="2" text="def"></div>
    <my-item>
        <div id="21" text="def-1"></div>
        <my-item>
            <div id="211" text="def-1-1"></div>
        </my-item>
        <my-item>
            <div id="212" text="def-1-2"></div>
        </my-item>
    </my-item>
    <my-item>
        <div id="22" text="def-2"></div>
    </my-item>
</my-item>

我只能渲染第一级输入,如我的 plunk 中所示。如何使我的组件递归迭代以呈现所需的输出?输入数组可以包含任意数量的元素和嵌套。

谢谢!

【问题讨论】:

    标签: javascript arrays recursion angular


    【解决方案1】:

    更新

    随着@NgModule 的引入以及directives@NgModule 的迁移,forwardRef 应该不再需要了。

    原创

    <my-item *ngFor="let itm of items" [id]="itm.id" [text]="itm.text" [items]="itm.items" >
    </my-item>
    
    @Component({
      selector: 'my-item',
      styles: ['div {padding-left: 32px;}']
      providers: [],
      template: `<div>{{id}} - {{text}}
        <my-item *ngFor="let itm of items" [id]="itm.id" [text]="itm.text" [items]="itm.items" >
        </my-item>
        </div>
    
      `,
      directives: [forwardRef(()=> ItemComponent)]
    })
    export class ItemComponent {
      @Input() id: string;
      @Input() text: string;
      @Input() items: any[];
      constructor() {
      }
    }
    

    forwardRef 是必需的,因为在声明类 ItemComponent 之前它已被引用。

    Plunker example

    【讨论】:

      【解决方案2】:

      我会这样创建你的组件:

      @Component({
        selector: 'my-item',
        template: `
          <div id="data.id" text="data.def"></div>
          <my-item *ngFor="let item of items" [data]="item"></my-item>
        `
      })
      export class MyItemComponent {
        @Input() data: any;
      }
      

      并从另一个组件中调用它并提供整个组件数据:

      @Component({
        selector: 'other-comp',
        template: `
          <my-item [data]="data" *ngFor="let data of wholeData"></my-item>
        `
      })
      export class OtherComponent {
        wholeData = [
          {
            "id": 1,
            "text": "abc"
          },
          (...)
        ];
      }
      

      【讨论】:

      • 这是否会通过所有嵌套元素递归地加载整个数组?好像只能到2级了。
      【解决方案3】:

      这里有一些东西可以帮助您入门。 HTML返回函数需要自己实现:

        var a = [
        {
          "id": 1,
          "text": "abc"
        },
        {
          "id": 2,
          "text": "def",
          items: [
            {
              "id": 21,
              "text": "def-1",
              items: [
                {
                  "id": 211,
                  "text": "def-1-1"
                },
                {
                  "id": 212,
                  "text": "def-1-2"
                }
              ]
            },
            {
              "id": 22,
              "text": "def-2"
            }
          ]
        }
      ];
      
      function iterateRecursive(array) {
          for(var i = 0; i < array.length; i++) {
          if(array[i].items) {
              console.log(array[i].text);
            //uncomment: printHtml(array[i].id, array[i].text);
              iterateRecursive(array[i].items)
          } else {    
              console.log(array[i].text);         
          }
      
        }
      }
      
      
      iterateRecursive(a);
      
      //implement this function that takes an id and text and returns html
      //call it in place of console log
      function printHtml(id, text) {
      
      }
      

      【讨论】:

      • 但是我需要在 Angular 2 组件加载中解决这个问题,而不仅仅是使用 javascript。在 javascript 中遍历递归数组是没有问题的。问题是通过 Angular 2 组件渲染输出。
      • Niner 也说过,您的解决方案与 Angular2+ 无关。
      猜你喜欢
      • 2019-10-08
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2013-11-21
      相关资源
      最近更新 更多