【问题标题】:Not able to display tree structure无法显示树结构
【发布时间】:2019-03-13 14:27:16
【问题描述】:

我正在尝试在 UI 上显示树结构。但是得到一个错误。

无法绑定到“target”,因为它不是“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>

我无法找出导致此故障的原因。我已经尝试了所有可能的条件来使其发挥作用。请帮我解决这个问题

【问题讨论】:

标签: angular angular-template


【解决方案1】:

Angular 期望 &lt;i&gt; 元素有一个名为 target 的输入。因为您使用的是data-target,这是[target] 的简写符号。

将您的 html 更改为:

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

这样你绑定到一个属性,而不是一个属性或输入绑定

【讨论】:

  • 感谢您的帮助
猜你喜欢
  • 2022-12-28
  • 2017-04-18
  • 2020-09-04
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
相关资源
最近更新 更多