How to set dynamic id
in Angular 2?
I have tried:
<div class = "CirclePoint" *ngFor="#c of circles" id = "{{ 'Location' + c.id }}"></div>
this.circles = [
{ x: 50 , y: 50 ,id : "oyut1" },
{ x: 100 , y: 100 ,id : "oyut3" },
{ x: 150 , y: 150 ,id : "oyut2" }
];
but it does not work.
<div class = "CirclePoint" *ngFor="let c of circles"
[attr.id]="'Location' + c.id">
</div>
<div class = "CirclePoint" *ngFor="let c of circles"
attr.id="Location{{c.id}}">
</div>
This also will work:
<div class = "CirclePoint" *ngFor="let c of circles">
<div [id]="c.id"></div>
</div>
If you want to add a prefix, say "loc";
<div class = "CirclePoint" *ngFor="let c of circles">
<div [id]="'loc' + c.id"></div>
</div>
Using [] helps to add values dynamically.
Try this:
<div class = "CirclePoint" *ngFor="let c of circles">
<div id="location_{{c.id}}">write something which you want like c.x </div>
</div>`
Hopefully this will work for you. I searched StackOverflow and I found this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With