1

I am trying to display a tree structure on UI. But getting an error.

Can't bind to 'target' since it isn't a known property of 'i'. ("v *ngFor="let item of files"> ]data-target="#{{item.reference}}" class="fa fa-caret-right" *ngIf="!(item.children.length==="): ng:///AppModule/SidenavComponent.html@9:34

Though this property exists in my json file as below:

[
  {
    "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": []
              }
            ]
          }
        ]
      }
    ]
  }
]

And my html file is as follows:

<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>

I am not able to figure out the cause for this failure. I have tried with all possible conditions I could apply to make it work. Please help me resolving this

VIBHOR GOYAL
  • 473
  • 1
  • 6
  • 22
  • Possible duplicate of [How to bind to data-\* attribute in angular2?](https://stackoverflow.com/questions/34412103/how-to-bind-to-data-attribute-in-angular2) – zmag Mar 13 '19 at 14:35

1 Answers1

0

Angular is expecting the <i> element to have an input named target. Because you are using data-target, which is a longhand notation for [target].

Change your html to:

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

This way you bind to an attribute, and not to a property or input binding

Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149