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