Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add dynamic index from ngFor to html attribute value

Tags:

html

angular

I run ngFor and I need that some attribute inside the loop would change it's value by adding to it ngFor index. That's mean that each div that is created in ngFor will have uniq attribute value. Source :

<div class="class1" *ngFor="let item of items; let i= index">
    <div class="card-header" role="tab" id="Id">
        <h1>Hello</h1>
    </div>

I want to bind id to get it's value: Id0 when index=0.

<div class="card-header" role="tab" [attr.id]="Id+'i'"> Doesn't work :(
like image 457
user3035279 Avatar asked Mar 16 '17 14:03

user3035279


2 Answers

Try:

<div class="card-header" role="tab" id="{{'Id'+i}}">
like image 87
snorkpete Avatar answered Oct 03 '22 19:10

snorkpete


3 solutions

<div class="card-header" role="tab" id="{{'Id'+i}}">
<div class="card-header" role="tab" [attr.id]="'Id'+i">
<div class="card-header" role="tab" [id]="'Id'+i">
like image 36
Stéphane GRILLON Avatar answered Oct 03 '22 17:10

Stéphane GRILLON