如何在ionic2 / angular2中设置动态ID(* ngFor)?

时间:2016-01-02 17:15:47

标签: angular ionic2

如何在angular2中设置动态ID?

我试过了

library(data.table)
setDT(df)[, list(P= mean(P)) , .(JB, Y)]

但它不起作用。

5 个答案:

答案 0 :(得分:94)

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

<击> 对于id属性,这可能也有效(尚未尝试过)

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

<击>

答案 1 :(得分:2)

这也将起作用:

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

如果要添加前缀,请说“ loc”;

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

使用[]有助于动态添加值。

答案 2 :(得分:1)

试试这个:

 <div class = "CirclePoint" *ngFor="let c of circles">
     <div id="location_{{c.id}}">write something which you want like c.x </div>
 </div>`

希望这对你有用。我搜索了StackOverflow,发现了这个answer

答案 3 :(得分:0)

在组件代码中,而不是通常的id="#c"使用[id]="#c"。这也适用于动态类名。见下面的例子:

<div class = "CirlePoit" *ngFor="#c of circles" [id] = "#c"> </div>

答案 4 :(得分:0)

<块引用>

索引可以用作动态 ID

<div class="test" *ngFor="let item of data; let i = index">
<div class="function_div" (click)="test(i);">Test Button </div>
    {{item}}
</div>