How to set dynamic id(*ngFor) in ionic2/angular2?

2019-02-01 16:06发布

How to set dynamic id in angular2?

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.

3条回答
老娘就宠你
2楼-- · 2019-02-01 16:37

Try this:

 <div class = "CirclePoint" *ng-for="#c in 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.

查看更多
做个烂人
3楼-- · 2019-02-01 16:41
<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>

For the id attribute this might work as well (not tried myself yet)

<div class = "CirclePoint" *ngFor="let c of circles" 
[id]="'Location' + c.id">
</div>

查看更多
对你真心纯属浪费
4楼-- · 2019-02-01 16:51

Inside the component tag instead of the usual id="#c" use [id]="#c". This also applies to dynamic class names. See example below:

<div class = "CirlePoit" *ngFor="#c of circles" [id] = "#c"> </div>
查看更多
登录 后发表回答