Can't bind to 'target' since it isn't a known property of 'div'
AngularCollapseAngular Problem Overview
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.
Angular Solutions
Solution 1 - Angular
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";
}
Solution 2 - Angular
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>
Solution 3 - Angular
use property binding : attr.data-target="{{your-target}}"
Solution 4 - Angular
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>
Solution 5 - Angular
<ng-container matColumnDef="opciones">
<th mat-header-cell *matHeaderCellDef> Opciones </th>
<td mat-cell *matCellDef="let item" class="text-center" role="button">
<a [routerLink]="['/panel/clientes',item._id]" matTooltip="Editar"><i class="bi bi-pencil-square"></i></a>
<a role="button" data-bs-toggle="modal" href="#delete-{{item._id}}" matTooltip="Borrar"><i class="bi bi-trash-fill"></i></a>
<!-- Modal -->
<div class="modal fade" id="delete-{{item._id}}" tabindex="-1" aria-hidden="true">
....**strong text**
</div>
</td>
</ng-container>