76

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Er Vipin Sharma
  • 2,519
  • 8
  • 22
  • 32

5 Answers5

170

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

  }
msanford
  • 11,803
  • 11
  • 66
  • 93
Aravind
  • 40,391
  • 16
  • 91
  • 110
51

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>
Amit
  • 4,274
  • 21
  • 25
  • 14
    Just to make things clear, it doesn't count as a duplicate of your answer if you edit it after the fact. This was my answer before you edited, not that I care. – Amit Aug 13 '17 at 20:49
2

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

Achraf Farouky
  • 813
  • 10
  • 11
-1

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>
Bhaskararao Gummidi
  • 2,513
  • 1
  • 12
  • 15
-2
<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>&nbsp;
  <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>
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '21 at 21:30