无法显示树状结构

时间:2019-03-13 14:27:17

标签: angular angular-template

我正在尝试在UI上显示树结构。但是出现错误。

  

不能绑定到“目标”,因为它不是“ i”的已知属性。 (“ v   * ngFor =“让文件项”>                    ] data-target =“#{{item.reference}}”“           class =“ fa fa-caret-right” * ngIf =“!(item.children.length ===”):ng:///AppModule/SidenavComponent.html@9:34

尽管此属性存在于我的json文件中,如下所示:

[
  {
    "name": "great grandparent",
    "reference": "great_grandparent",
    "children": [
      {
        "name": "childless grandsibiling",
        "reference": "childless_grandsibiling",
        "children": []
      },
      {
        "name": "grandparent",
        "reference": "grandparent",
        "children": [
          {
            "name": "childless sibiling",
            "reference": "childless_sibling",
            "children": []
          },
          {
            "name": "another childless sibiling",
            "reference": "another_childless_sibiling",
            "children": []
          },
          {
            "name": "parent",
            "reference": "parent",
            "children": [
              {
                "name": "child",
                "reference": "child",
                "children": []
              },
              {
                "name": "another child",
                "reference": "another_child",
                "children": []
              }
            ]
          }
        ]
      }
    ]
  }
]

我的html文件如下:

<div class="row">
  <div class="col-md-12">
    <div id="google_translate_element"></div>
  </div>
</div>
<div class="container sidenav-tree">
  <ng-template #recursiveList let-files>
    <div *ngFor="let item of files">
      <div class="row node-item">
        <i data-toggle="collapse" data-target="#{{item.reference}}"
        class="fa fa-caret-right" *ngIf="!(item.children.length===0)"></i>
        {{item.name}}
      </div>
      <div id="{{item.reference}}" class="container collapse" *ngIf="!(item.children.length===0)">
        <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: item.children }"></ng-container>
      </div>
    </div>
  </ng-template>
  <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: files }"></ng-container>
</div>

我无法找出导致此失败的原因。我已经尝试了所有可能适用的条件以使其正常运行。请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

Angular期望<i>元素具有名为target的输入。因为您使用的是data-target,所以它是[target]的缩写。

将您的html更改为:

<i data-toggle="collapse" attr.data-target="#{{item.reference}}"

通过这种方式,您可以绑定到属性,而不是属性或输入绑定