Can't bind to 'target' since it isn't a known property of 'div'

China☆狼群 提交于 2019-12-18 10:44:38

问题


I am getting this error while implementing collapse feature:

Error: Template parse errors: Can't bind to 'target' since it isn't a known property of 'div'

app.component.html:

<div *ngFor = "let ele of elements; let RowIndex = index">
    {{ele.name}} 
    <button data-toggle="collapse" 
            data-target="#demo{{RowIndex}}">Toggle
    </button>
    <div id="demo{{RowIndex}}" class="collapse">Lorem Ipsum</div>

</div>

But if I simply use data-target="#demo" , that is working fine. But when I am binding {{RowIndex}} than its showing error.


回答1:


You missed property binding

<button data-toggle="collapse" 
        [attr.data-target]="'#demo'+ RowIndex">Toggle
</button>


<button (click)="clickMe($event)">Toggle</button>

clickMe(value){
    value.srcElement.innerHTML="Clicked";

  }



回答2:


Use angular's attribute binding syntax.

Use one of the following:

<button data-toggle="collapse" 
        attr.data-target="#demo{{RowIndex}}">Toggle
</button>

or

<button data-toggle="collapse" 
        [attr.data-target]="'#demo' + RowIndex">Toggle
</button>



回答3:


use property binding : attr.data-target="{{your-target}}"




回答4:


You can use href tag instead of div. you can check the below code

<div class="card" *ngFor="let service of servicesArr;let i = index">
  <a data-toggle="collapse" href="#{{i}}{{service.name}}">{{service.label}}</a>
  <div id="{{i}}{{service.name}}" class="collapse">
     Lorem ipsum dolor text....
  </div>
</div>


来源:https://stackoverflow.com/questions/43386590/cant-bind-to-target-since-it-isnt-a-known-property-of-div

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!