如何将click事件绑定到angular2中循环中div的特定id

时间:2017-10-21 16:43:38

标签: angular

如何更改id =" itemid0"的文字当id =" buttonitemid0"点击了吗?

app.component.ts
-----------------
   bb = 'cat';
changeText(index){

}
     app.component.html
    -----------------------------

        <div class="col-xs-2" *ngFor="let item of items;let k=index;">
        <div class="col-xs-12" id="itemid{{index}}">aa</div>
        <button type="button" id="buttonitemid{{index}}" (click)="changeText(index)">btnn</button>
    </div>

1 个答案:

答案 0 :(得分:0)

只需将索引存储在组件的字段中,然后绑定到此字段

即可

app.component.ts

clickedIdx :number;
changeText(index){
  this.clickedIdx = index;
}

app.component.html

<div class="col-xs-2" *ngFor="let item of itmes"></div>
    <div class="col-xs-2" *ngFor="let item of items;let k=index;">
    <div class="col-xs-12" id="itemid{{index}}">aa</div>
    <button type="button" id="buttonitemid{{clickedIdx}}" (click)="changeText(k)">btnn</button>
</div>