2016-01-02 15 views
37

angle2で動的IDを設定するにはどうすればよいですか?ionic2/angular2の動的ID(* ngFor)の設定方法は?

私は

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

を試してみましたが、それは動作しません。

<div class = "CirclePoint" *ng-for="#c in circles"> 
    <div id="location_{{c.id}}">write something which you want like c.x </div> 
</div>` 

うまくいけば、これはあなたのために動作します:

答えて

60
<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="#c"使用 [id]="#c"の(まだ自分自身を試していない)だけでなくコンポーネントタグ内

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

+1

ありがとうございました。 [attr.id]は動作しますが、[id]は動作しません。 –

+0

フィードバックをいただきありがとうございます、私は特別な扱いでいくつかの共通の属性があったと思ったが、多分何かを混ぜた。 –

+0

私が探していたもの。そこにはさまざまな方法がありますし、角度2が変わるように正しい方法が変わるようですね。 – John

2

はこれを試してみてください。 StackOverflowを検索したところ、これはanswerでした。 idについては

+0

ありがとうございます。しかし、私はangular2に取り組んでいます。 "ng-attr-id"はもう動作しません。 –

+0

申し訳ありませんが、私はあなたのタグを見ることなくangular2で動作しません。私はあなたから応答しました。ネットからの回答が見つかりました –

0

を働くかもしれない属性。これは動的クラス名にも当てはまります。下記の例をご覧ください:

<div class = "CirlePoit" *ngFor="#c of circles" [id] = "#c"> </div> 
関連する問題